<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Node.js,数据库,MySQL,MongoDB," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="&amp;#x6570;&amp;#x636E;&amp;#x5E93;&amp;#xFF1A;&amp;#x5B58;&amp;#x50A8;&amp;#x6570;&amp;#x636E;&amp;#x7684;&amp;#x4ED3;&amp;#x5E93;&amp;#x3002;

&amp;#x6570;&amp;#x636E;&amp;#x7ED3;&amp;#x6784;&amp;#x5316;
&amp;#x5B9E;&amp;#x73B0;&amp;#x6570;&amp;#x636E;&amp;#x5171;&amp;#x4EAB;
&amp;#x53EF;&amp;#x4">
<meta property="og:type" content="article">
<meta property="og:title" content="数据库基础 以及 Node 操作方法">
<meta property="og:url" content="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="&amp;#x6570;&amp;#x636E;&amp;#x5E93;&amp;#xFF1A;&amp;#x5B58;&amp;#x50A8;&amp;#x6570;&amp;#x636E;&amp;#x7684;&amp;#x4ED3;&amp;#x5E93;&amp;#x3002;

&amp;#x6570;&amp;#x636E;&amp;#x7ED3;&amp;#x6784;&amp;#x5316;
&amp;#x5B9E;&amp;#x73B0;&amp;#x6570;&amp;#x636E;&amp;#x5171;&amp;#x4EAB;
&amp;#x53EF;&amp;#x4">
<meta property="og:image" content="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/连接池.png">
<meta property="og:updated_time" content="2016-12-01T12:09:30.789Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据库基础 以及 Node 操作方法">
<meta name="twitter:description" content="&amp;#x6570;&amp;#x636E;&amp;#x5E93;&amp;#xFF1A;&amp;#x5B58;&amp;#x50A8;&amp;#x6570;&amp;#x636E;&amp;#x7684;&amp;#x4ED3;&amp;#x5E93;&amp;#x3002;

&amp;#x6570;&amp;#x636E;&amp;#x7ED3;&amp;#x6784;&amp;#x5316;
&amp;#x5B9E;&amp;#x73B0;&amp;#x6570;&amp;#x636E;&amp;#x5171;&amp;#x4EAB;
&amp;#x53EF;&amp;#x4">
<meta name="twitter:image" content="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/连接池.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/"/>


  <title> 数据库基础 以及 Node 操作方法 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                数据库基础 以及 Node 操作方法
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-10-27T13:16:55+08:00" content="2016-10-27">
              2016-10-27
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/MySQL/" itemprop="url" rel="index">
                    <span itemprop="name">MySQL</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/10/27/s011-数据库-MySQL/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/10/27/s011-数据库-MySQL/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#x6570;&#x636E;&#x5E93;&#xFF1A;&#x5B58;&#x50A8;&#x6570;&#x636E;&#x7684;&#x4ED3;&#x5E93;&#x3002;</p>
<ul>
<li>&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5316;</li>
<li>&#x5B9E;&#x73B0;&#x6570;&#x636E;&#x5171;&#x4EAB;<ul>
<li>&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x5197;&#x4F59;&#x6570;&#x636E;</li>
</ul>
</li>
<li>&#x6570;&#x636E;&#x72EC;&#x7ACB;&#x6027;&#x9AD8;</li>
<li>&#x6570;&#x636E;&#x7EDF;&#x4E00;&#x7BA1;&#x7406;&#x4E0E;&#x63A7;&#x5236;</li>
</ul>
<h2 id="&#x6570;&#x636E;&#x5E93;&#x4ECB;&#x7ECD;"><a href="#&#x6570;&#x636E;&#x5E93;&#x4ECB;&#x7ECD;" class="headerlink" title="&#x6570;&#x636E;&#x5E93;&#x4ECB;&#x7ECD;"></a>&#x6570;&#x636E;&#x5E93;&#x4ECB;&#x7ECD;</h2><ul>
<li>MySQL &#x6570;&#x636E;&#x5E93;</li>
<li>Oracle<ul>
<li>Orcale&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;&#x7531;&#x7532;&#x9AA8;&#x6587;&#x516C;&#x53F8;&#x5F00;&#x53D1;&#xFF0C;&#x5728;&#x6570;&#x636E;&#x5E93;&#x9886;&#x57DF;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x9886;&#x5148;&#x5730;&#x4F4D;</li>
<li>&#x5546;&#x4E1A;&#x6536;&#x8D39;&#xFF0C;&#x5927;&#x578B;&#x7CFB;&#x7EDF;&#xFF0C;&#x6DD8;&#x5B9D;&#x3001;&#x4EAC;&#x4E1C;&#x5C31;&#x4F7F;&#x7528;&#x4E86; Oracle &#x6570;&#x636E;&#x5E93;</li>
</ul>
</li>
<li>SqlServer &#x6570;&#x636E;&#x5E93;<ul>
<li>&#x7531;&#x5FAE;&#x8F6F;&#x516C;&#x53F8;&#x5F00;&#x53D1;&#xFF0C;&#x53EA;&#x80FD;&#x5728;Windows&#x4E0A;&#x8FD0;&#x884C;&#x3002;.net&#x5F00;&#x53D1;&#x7A0B;&#x5E8F;&#x5458;</li>
</ul>
</li>
<li>DB2<ul>
<li>&#x7531;IBM&#x516C;&#x53F8;&#x5F00;&#x53D1;&#xFF0C;&#x5728;&#x91D1;&#x878D;&#x884C;&#x4E1A;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x6BD4;&#x8F83;&#x591A;&#x3002;IBM&#x7684;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x4E70;&#x670D;&#x52A1;&#x5668;&#x9001;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x5546;&#x4E1A;&#x6536;&#x8D39;</li>
</ul>
</li>
<li>MS SQL Server<ul>
<li>&#x4EE5;&#x4E0A;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x5E93;&#x90FD;&#x662F;&#x4F7F;&#x7528; SQL &#x8BED;&#x8A00;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;</li>
<li>SQL &#x8BED;&#x8A00;&#x5C31;&#x662F;&#x7528;&#x4E8E; &#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93; &#x7684;&#x4E00;&#x4E2A;&#x64CD;&#x4F5C;&#x8BED;&#x8A00;</li>
<li>&#x5229;&#x7528;&#x8FD9;&#x4E2A; SQL &#x8BED;&#x8A00;&#x53EF;&#x4EE5;&#x589E;&#x5220;&#x6539;&#x67E5;&#x5E93;&#x548C;&#x8868;&#xFF0C;&#x4EE5;&#x53CA;&#x589E;&#x5220;&#x6539;&#x67E5;&#x8868;&#x6570;&#x636E;</li>
<li>&#x8868; &#x5C31;&#x662F; &#x5173;&#x7CFB;</li>
<li>&#x8868;&#x4E0E;&#x8868;&#x4E4B;&#x95F4;&#x5C31;&#x662F;&#x5173;&#x7CFB;</li>
</ul>
</li>
<li><p>MongoDB</p>
<ul>
<li>&#x975E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x6CA1;&#x6709;&#x8868;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x5B58;&#x50A8;&#x7684;&#x6570;&#x636E;&#x90FD;&#x5728;&#x96C6;&#x5408;&#x4E2D;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E; JavaScript &#x5BF9;&#x8C61;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F; json &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;</li>
<li>&#x96C6;&#x5408;&#x6CA1;&#x6709;&#x7ED3;&#x6784;&#xFF0C;&#x96C6;&#x5408;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;</li>
<li>&#x4E5F;&#x53EF;&#x4EE5;&#x5F80;&#x96C6;&#x5408;&#x4E2D;&#x63D2;&#x5165;&#x6570;&#x636E;</li>
</ul>
</li>
<li><p>&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x5B58;&#x50A8;&#x7ED3;&#x6784;</p>
<ul>
<li>&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;<ul>
<li>&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;</li>
</ul>
</li>
<li>&#x6570;&#x636E;&#x5E93;</li>
<li>&#x6570;&#x636E;&#x8868;<ul>
<li>&#x6839;&#x636E;&#x4E1A;&#x52A1;&#x8BBE;&#x8BA1;&#x8868;&#x7ED3;&#x6784;</li>
</ul>
</li>
<li>&#x8BB0;&#x5F55;<ul>
<li>&#x6839;&#x636E;&#x8868;&#x7ED3;&#x6784;&#x5B58;&#x50A8;&#x8BB0;&#x5F55;&#x6570;&#x636E;</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="MongoDB"><a href="#MongoDB" class="headerlink" title="MongoDB"></a>MongoDB</h2><p>MongoDB&#x662F;&#x7531;10gen&#x516C;&#x53F8;&#x5F00;&#x53D1;&#x7684;&#x4E00;&#x4E2A;&#x4ECB;&#x4E8E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x548C;&#x975E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x4E4B;&#x95F4;&#x7684;&#x4EA7;&#x54C1;&#xFF0C;&#x662F;&#x975E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x529F;&#x80FD;&#x6700;&#x4E30;&#x5BCC;&#xFF0C;&#x6700;&#x50CF;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x7684;&#x3002;&#x4ED6;&#x652F;&#x6301;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x975E;&#x5E38;&#x677E;&#x6563;&#xFF0C;&#x662F;&#x7C7B;&#x4F3C;json&#x7684;&#x683C;&#x5F0F;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7C7B;&#x578B;&#x3002;MongoDB&#x6570;&#x636E;&#x5E93;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;&#x6700;&#x5927;&#x7684;&#x7279;&#x70B9;&#x5C31;&#x662F;&#x5B83;&#x652F;&#x6301;&#x7684;&#x67E5;&#x8BE2;&#x8BED;&#x8A00;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#xFF0C;&#x8BED;&#x6CD5;&#x7C7B;&#x4F3C;&#x4E8E;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x67E5;&#x8BE2;&#x8BED;&#x8A00;&#x3002;&#x5B83;&#x8FD8;&#x662F;&#x4E00;&#x4E2A;&#x5F00;&#x6E90;&#x7684;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5BF9;&#x4E8E;&#x5927;&#x6570;&#x636E;&#x91CF;&#x3001;&#x9AD8;&#x5E76;&#x53D1;&#x7684;&#x4E92;&#x8054;&#x7F51;&#x5E94;&#x7528;&#xFF0C;&#x652F;&#x6301;&#x975E;&#x5E38;&#x4E0D;&#x9519;&#x3002;&#x64CD;&#x4F5C;&#x975E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;SQL&#x8BED;&#x8A00;&#x3002;</p>
<p>MongoDB &#x5B98;&#x7F51;&#xFF1A;<a href="https://www.mongodb.com/" target="_blank" rel="external">https://www.mongodb.com/</a></p>
<ul>
<li>MongoDB &#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x9762;&#x5411;&#x6587;&#x6863;&#x5B58;&#x50A8;</li>
<li>&#x5F00;&#x6E90;&#x514D;&#x8D39;&#x3001;&#x9AD8;&#x6027;&#x80FD;</li>
<li>MongoDB &#x5C06;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x4E3A;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#xFF0C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7531;&#x952E;&#x503C;(key=&gt;value)&#x5BF9;&#x7EC4;&#x6210;</li>
<li>MongoDB &#x6587;&#x6863;&#x7C7B;&#x4F3C;&#x4E8E; JSON &#x5BF9;&#x8C61;</li>
<li>&#x5B57;&#x6BB5;&#x503C;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x5176;&#x4ED6;&#x6587;&#x6863;&#xFF0C;&#x6570;&#x7EC4;&#x53CA;&#x6587;&#x6863;&#x6570;&#x7EC4;</li>
<li>Mongo&#x652F;&#x6301;&#x4E30;&#x5BCC;&#x7684;&#x67E5;&#x8BE2;&#x8868;&#x8FBE;&#x5F0F;</li>
<li>&#x67E5;&#x8BE2;&#x6307;&#x4EE4;&#x4F7F;&#x7528;JSON&#x5F62;&#x5F0F;&#x7684;&#x6807;&#x8BB0;&#xFF0C;&#x53EF;&#x8F7B;&#x6613;&#x67E5;&#x8BE2;&#x6587;&#x6863;&#x4E2D;&#x5185;&#x5D4C;&#x7684;&#x5BF9;&#x8C61;&#x53CA;&#x6570;&#x7EC4;&#x3002;</li>
</ul>
<h3 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x76EE;&#x5F55;&#x5E76;&#x542F;&#x52A8;MongoDB&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;"><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x76EE;&#x5F55;&#x5E76;&#x542F;&#x52A8;MongoDB&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;" class="headerlink" title="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x76EE;&#x5F55;&#x5E76;&#x542F;&#x52A8;MongoDB&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;"></a>&#x521B;&#x5EFA;&#x6570;&#x636E;&#x76EE;&#x5F55;&#x5E76;&#x542F;&#x52A8;MongoDB&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;</h3><blockquote>
<p>&#x542F;&#x52A8;MongoDB&#x6570;&#x636E;&#x5E93;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x76EE;&#x5F55;</p>
</blockquote>
<ul>
<li>&#x5728; c &#x76D8; &#x6839;&#x76EE;&#x5F55;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#xFF1A;data</li>
<li>&#x63A5;&#x4E0B;&#x6765;&#x542F;&#x52A8; MongoDB &#x670D;&#x52A1;&#x5668;<ul>
<li>&#x6253;&#x5F00;&#x7EC8;&#x7AEF;&#xFF0C;&#x8F93;&#x5165;&#xFF1A;<code>mongod &#x4F60;&#x521A;&#x624D;&#x521B;&#x5EFA;&#x7684;&#x76EE;&#x5F55;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</code></li>
</ul>
</li>
</ul>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x53D1;&#x73B0;&#x6267;&#x884C;&#x4E0A;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x6536;&#x5230;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x63D0;&#x793A;&#xFF1A;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&apos;mongod&apos; &#x4E0D;&#x662F;&#x5185;&#x90E8;&#x6216;&#x5916;&#x90E8;&#x547D;&#x4EE4;&#xFF0C;&#x4E5F;&#x4E0D;&#x662F;&#x53EF;&#x8FD0;&#x884C;&#x7684;&#x7A0B;&#x5E8F;&#x6216;&#x6279;&#x5904;&#x7406;&#x6587;&#x4EF6;&#x3002;</div></pre></td></tr></table></figure>
<p>&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF1A;&#x5C06; MongoDB &#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E0B;&#x7684; bin &#x76EE;&#x5F55;&#x914D;&#x7F6E;&#x5230;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6267;&#x884C;&#x5B8C;&#x4E0A;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x542F;&#x52A8;&#x6210;&#x529F;&#x540E;&#x4E0D;&#x8981;&#x52A8;&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#x53F0;&#x4E86;&#xFF0C;&#x8BF4;&#x660E; MongoDB &#x670D;&#x52A1;&#x5668;&#x6B63;&#x5728;&#x8FD0;&#x884C;&#x3002;</p>
<p>MongoDB &#x9ED8;&#x8BA4;&#x8FD0;&#x884C;&#x7684;&#x7AEF;&#x53E3;&#x53F7;&#x662F; 27017</p>
<p>&#x5982;&#x4F55;&#x5173;&#x95ED;&#xFF1F;</p>
<p>&#x5728;&#x7EC8;&#x7AEF;&#x4E2D;&#xFF1A;<code>Ctrl + C</code> &#x5173;&#x95ED; MongoDB &#x6570;&#x636E;&#x5E93;&#x3002;</p>
<h3 id="&#x8FDE;&#x63A5;-MongoDB-&#x6570;&#x636E;&#x5E93;"><a href="#&#x8FDE;&#x63A5;-MongoDB-&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x8FDE;&#x63A5; MongoDB &#x6570;&#x636E;&#x5E93;"></a>&#x8FDE;&#x63A5; MongoDB &#x6570;&#x636E;&#x5E93;</h3><h3 id="&#x5B58;&#x50A8;&#x7ED3;&#x6784;"><a href="#&#x5B58;&#x50A8;&#x7ED3;&#x6784;" class="headerlink" title="&#x5B58;&#x50A8;&#x7ED3;&#x6784;"></a>&#x5B58;&#x50A8;&#x7ED3;&#x6784;</h3><ul>
<li>&#x6570;&#x636E;&#x5E93;<ul>
<li>&#x4E00;&#x4E2A;mongodb&#x4E2D;&#x53EF;&#x4EE5;&#x5EFA;&#x7ACB;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;</li>
<li>MongoDB&#x7684;&#x9ED8;&#x8BA4;&#x6570;&#x636E;&#x5E93;&#x4E3A;&#x201D;db&#x201D;&#xFF0C;&#x8BE5;&#x6570;&#x636E;&#x5E93;&#x5B58;&#x50A8;&#x5728;data&#x76EE;&#x5F55;&#x4E2D;</li>
<li>MongoDB&#x7684;&#x5355;&#x4E2A;&#x5B9E;&#x4F8B;&#x53EF;&#x4EE5;&#x5BB9;&#x7EB3;&#x591A;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A;&#x90FD;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x96C6;&#x5408;&#x548C;&#x6587;&#x6863;</li>
<li>&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x5E93;&#x4E5F;&#x653E;&#x7F6E;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x6587;&#x4EF6;&#x4E2D;</li>
<li><code>show dbs</code> &#x663E;&#x793A;&#x6240;&#x6709;&#x6570;&#x636E;&#x5E93;&#x5217;&#x8868;</li>
<li><code>db</code> &#x663E;&#x793A;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;&#x5BF9;&#x8C61;&#x6216;&#x96C6;&#x5408;</li>
<li><code>use &#x6570;&#x636E;&#x5E93;&#x540D;&#x5B57;</code> &#x8FDE;&#x63A5;&#x5230;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x6570;&#x636E;&#x5E93;</li>
</ul>
</li>
<li>&#x96C6;&#x5408;<ul>
<li>&#x96C6;&#x5408;&#x5C31;&#x662F; MongoDB &#x6587;&#x6863;&#x7EC4;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x8868;</li>
<li>&#x96C6;&#x5408;&#x5B58;&#x5728;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#xFF0C;&#x96C6;&#x5408;&#x6CA1;&#x6709;&#x56FA;&#x5B9A;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A; JSON</li>
<li>&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x6211;&#x4EEC;&#x63D2;&#x5165;&#x96C6;&#x5408;&#x7684;&#x6570;&#x636E;&#x90FD;&#x4F1A;&#x6709;&#x4E00;&#x5B9A;&#x7684;&#x5173;&#x8054;&#x6027;&#x3002;</li>
<li>&#x5F53;&#x7B2C;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#x63D2;&#x5165;&#x65F6;&#xFF0C;&#x96C6;&#x5408;&#x5C31;&#x4F1A;&#x88AB;&#x521B;&#x5EFA;&#x3002;</li>
<li>db.col.find() &#x67E5;&#x8BE2;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li>db.col.findOne({}) &#x67E5;&#x8BE2;&#x6307;&#x5B9A;&#x6570;&#x636E;</li>
</ul>
</li>
<li>&#x6587;&#x6863;<ul>
<li>&#x6587;&#x6863;&#x662F;&#x4E00;&#x4E2A;&#x952E;&#x503C;(key-value)&#x5BF9;(&#x5373;BSON)</li>
</ul>
</li>
</ul>
<h3 id="&#x57FA;&#x672C;&#x64CD;&#x4F5C;"><a href="#&#x57FA;&#x672C;&#x64CD;&#x4F5C;" class="headerlink" title="&#x57FA;&#x672C;&#x64CD;&#x4F5C;"></a>&#x57FA;&#x672C;&#x64CD;&#x4F5C;</h3><ul>
<li><code>show dbs</code> &#x67E5;&#x770B;&#x6570;&#x636E;&#x5E93;&#x5217;&#x8868;</li>
<li><code>use &#x540D;&#x79F0;</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;<ul>
<li>&#x8FD9;&#x91CC;&#x8868;&#x793A;&#x5982;&#x679C;&#x6709;&#x8BE5;&#x6570;&#x636E;&#x5E93;&#x5219;&#x5207;&#x6362;&#x5230;&#x8BE5;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#xFF08;&#x521B;&#x5EFA;&#x4E0D;&#x662F;&#x771F;&#x7684;&#x521B;&#x5EFA;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x4F60;&#x5728;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x63D2;&#x5165;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x6570;&#x636E;&#x4E4B;&#x540E;&#x624D;&#x4F1A;&#x521B;&#x5EFA;&#x6210;&#x529F;&#xFF09;</li>
</ul>
</li>
<li><code>show collections</code> &#x67E5;&#x770B;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x96C6;&#x5408;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;&#x79F0;.insert({&#x6587;&#x6863;&#x5BF9;&#x8C61;})</code> &#x5411;&#x96C6;&#x5408;&#x4E2D;&#x63D2;&#x5165;&#x4E00;&#x4E2A;&#x6587;&#x6863;&#xFF08;JSON &#x5BF9;&#x8C61;&#xFF09;<ul>
<li>&#x5982;&#x679C;&#x8BE5;&#x96C6;&#x5408;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x65B0;&#x5EFA;&#xFF0C;&#x5E76;&#x5C06;&#x6570;&#x636E;&#x63D2;&#x5165;&#x8FDB;&#x53BB;</li>
<li>&#x5982;&#x679C;&#x8BE5;&#x96C6;&#x5408;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x6570;&#x636E;&#x6DFB;&#x52A0;&#x5230;&#x8BE5;&#x96C6;&#x5408;&#x4E2D;</li>
</ul>
</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.find()</code> &#x67E5;&#x8BE2;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.find(&#x6587;&#x6863;&#x6761;&#x4EF6;)</code> &#x6309;&#x7167;&#x6587;&#x6863;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.findOne(&#x6587;&#x6863;&#x6761;&#x4EF6;)</code> &#x6309;&#x7167;&#x6587;&#x6863;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#xFF0C;&#x53EA;&#x67E5;&#x8BE2;&#x5339;&#x914D;&#x9879;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;</li>
<li><code>db.dropDatabase()</code> &#x81EA;&#x6740;&#xFF0C;&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.drop()</code> &#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x6307;&#x5B9A;&#x96C6;&#x5408;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.update(&#x6587;&#x6863;&#x6761;&#x4EF6;, &#x6587;&#x6863;&#x5BF9;&#x8C61;)</code> &#x6839;&#x636E;&#x6587;&#x6863;&#x6761;&#x4EF6;&#xFF0C;&#x6574;&#x4F53;&#x66F4;&#x65B0;&#x4E3A;&#x6587;&#x6863;&#x5BF9;&#x8C61;</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.update(&#x6587;&#x6863;&#x6761;&#x4EF6;, { $set:&#x6587;&#x6863;&#x5BF9;&#x8C61; })</code> &#x6839;&#x636E;&#x6587;&#x6863;&#x6761;&#x4EF6;&#xFF0C;&#x66F4;&#x65B0;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;<ul>
<li>&#x5982;&#x679C;&#x66F4;&#x65B0;&#x7684;&#x6587;&#x6863;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;&#x5DF2;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x66F4;&#x65B0;</li>
<li>&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x603C;&#x8FDB;&#x53BB;</li>
</ul>
</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.save(&#x6587;&#x6863;)</code><ul>
<li>&#x5982;&#x679C;&#x6587;&#x6863;&#x4E2D;&#x5305;&#x542B;&#x4E00;&#x4E2A; <code>&quot;_id&quot; : ObjectId(&quot;56064f89ade2f21f36b03136&quot;)</code> &#x8BE5;&#x5B57;&#x6BB5;</li>
<li>&#x5219;&#x4F1A;&#x627E;&#x5230;&#x5339;&#x914D;&#x9879;&#x6761;&#x4EF6;&#xFF0C;&#x6574;&#x4F53;&#x66FF;&#x6362;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x8BE5;&#x5B57;&#x6BB5;&#xFF0C;&#x5219;&#x76F4;&#x63A5; insert</li>
</ul>
</li>
<li><code>db.&#x96C6;&#x5408;&#x540D;.remove(&#x6761;&#x4EF6;&#x6587;&#x6863;)</code><ul>
<li><a href="http://www.runoob.com/mongodb/mongodb-operators.html" target="_blank" rel="external">http://www.runoob.com/mongodb/mongodb-operators.html</a> &#x6761;&#x4EF6;&#x64CD;&#x4F5C;&#x7B26;</li>
</ul>
</li>
</ul>
<h3 id="&#x5728;-Node-&#x4E2D;&#x64CD;&#x4F5C;-MongoDB"><a href="#&#x5728;-Node-&#x4E2D;&#x64CD;&#x4F5C;-MongoDB" class="headerlink" title="&#x5728; Node &#x4E2D;&#x64CD;&#x4F5C; MongoDB"></a>&#x5728; Node &#x4E2D;&#x64CD;&#x4F5C; MongoDB</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> mongodb = <span class="built_in">require</span>(<span class="string">&apos;mongodb&apos;</span>);</div><div class="line"><span class="keyword">var</span> MongoClient = mongodb.MongoClient;</div><div class="line"><span class="comment">// Connection URL </span></div><div class="line"><span class="keyword">var</span> url = <span class="string">&apos;mongodb://localhost:27017/itcast&apos;</span>;</div><div class="line"><span class="comment">// Use connect method to connect to the Server </span></div><div class="line">MongoClient.connect(url, <span class="function"><span class="keyword">function</span> (<span class="params">err, db</span>) </span>{</div><div class="line">  <span class="comment">// 2. &#x4F7F;&#x7528; db </span></div><div class="line">  <span class="comment">// &#x63D2;&#x5165;&#x6570;&#x636E;</span></div><div class="line">  <span class="comment">// db.collection(&apos;duanzi&apos;).insertMany([</span></div><div class="line">  <span class="comment">//   { nickname: &apos;&#x659C;&#x9633;&#x70DF;&#x67F3;&apos;, gender: 0, age: 20, content: &apos;&#x5C0F;&#x65F6;&#x5019;&#xFF0C;&#x6751;&#x91CC;&#x6709;&#x53EA;&#x72D7;&#x5F88;&#x51F6;&#xFF0C;&#x6211;&#x548C;&#x54E5;&#x54E5;&#x5BF9;&#x5B83;&#x53C8;&#x6068;&#x53C8;&#x6015;&#x3002;&#x4E00;&#x6B21;&#xFF0C;&#x6211;&#x54E5;&#x5728;&#x5BB6;&#x5077;&#x6211;&#x7238;&#x7684;&#x9152;&#x559D;&#x3002;&#x78B0;&#x5DE7;&#x90A3;&#x53EA;&#x72D7;&#x4ECE;&#x6211;&#x5BB6;&#x95E8;&#x524D;&#x7ECF;&#x8FC7;&#xFF0C;&#x6211;&#x54E5;&#x501F;&#x9152;&#x58EE;&#x80C6;&#xFF0C;&#x6284;&#x8D77;&#x68CD;&#x5B50;&#x51B2;&#x4E86;&#x51FA;&#x53BB;&#x3002;&#x51F6;&#x72D7;&#x89C1;&#x52BF;&#x4E0D;&#x5999;&#xFF0C;&#x4ED3;&#x7687;&#x9003;&#x7A9C;&#xFF0C;&#x6211;&#x54E5;&#x5C31;&#x6EE1;&#x6751;&#x8FFD;&#x7740;&#x6253;&#x3002;&#x7B2C;&#x4E8C;&#x5929;&#xFF0C;&#x6211;&#x54E5;&#x548C;&#x51F6;&#x72D7;&#x5728;&#x4E00;&#x6761;&#x7A84;&#x5DF7;&#x9047;&#x4E0A;&#xFF0C;&#x5F7C;&#x6B64;&#x4E00;&#x6123;&#xFF0C;&#x5404;&#x81EA;&#x626D;&#x5934;&#x6492;&#x817F;&#x5C31;&#x8DD1;...&apos;, }</span></div><div class="line">  <span class="comment">// ], (err, result) =&gt; {</span></div><div class="line">  <span class="comment">//   if (err) {</span></div><div class="line">  <span class="comment">//     throw err;</span></div><div class="line">  <span class="comment">//   }</span></div><div class="line">  <span class="comment">//   console.log(&apos;&#x63D2;&#x5165;&#x6570;&#x636E;&#x6210;&#x529F;&apos;);</span></div><div class="line">  <span class="comment">// });</span></div><div class="line"></div><div class="line">  <span class="comment">// &#x67E5;&#x8BE2;&#x6570;&#x636E;</span></div><div class="line">  db.collection(<span class="string">&apos;duanzi&apos;</span>).find({}).toArray(<span class="function">(<span class="params">err, docs</span>) =&gt;</span> {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    <span class="built_in">console</span>.log(docs);</div><div class="line">  })</div><div class="line">  db.close();</div><div class="line">});</div></pre></td></tr></table></figure>
<p>oracle&#x3001;sqlserver&#x3001;DB2&#x3001;MySQL &#x90FD;&#x662F;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<h2 id="&#x5B89;&#x88C5;&#x548C;&#x914D;&#x7F6E;-MySQL-&#x6570;&#x636E;&#x5E93;"><a href="#&#x5B89;&#x88C5;&#x548C;&#x914D;&#x7F6E;-MySQL-&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x5B89;&#x88C5;&#x548C;&#x914D;&#x7F6E; MySQL &#x6570;&#x636E;&#x5E93;"></a>&#x5B89;&#x88C5;&#x548C;&#x914D;&#x7F6E; MySQL &#x6570;&#x636E;&#x5E93;</h2><p>&#x5B98;&#x65B9;&#x7F51;&#x7AD9;&#xFF1A;<a href="http://www.mysql.com/" target="_blank" rel="external">http://www.mysql.com/</a></p>
<p>&#x542F;&#x52A8; &#x548C; &#x505C;&#x6B62;MySQL&#x670D;&#x52A1;&#xFF1A;</p>
<blockquote>
<p>&#x901A;&#x8FC7;Windows&#x7684;&#x8FD0;&#x884C;&#xFF0C;&#x8F93;&#x5165;services.msc&#x627E;&#x5230;MySQL&#x670D;&#x52A1;</p>
<p>&#x901A;&#x8FC7;DOS&#x547D;&#x4EE4;&#x542F;&#x52A8;MySQL&#x670D;&#x52A1;&#xFF08;&#x4F7F;&#x7528;&#x7BA1;&#x7406;&#x5458;&#x6743;&#x9650;&#x6253;&#x5F00;&#x7EC8;&#x7AEF;&#xFF09;</p>
</blockquote>
<p>&#x767B;&#x5F55;MySQL&#x6570;&#x636E;&#x5E93;:</p>
<p><code>mysql -h localhost -P 3306 -u root -p</code></p>
<ul>
<li><code>-h&#xFF1A;</code>&#x4E3B;&#x673A;&#x540D;</li>
<li><code>-P&#xFF1A;</code>&#x7AEF;&#x53E3;</li>
<li><code>-u&#xFF1A;</code>&#x7528;&#x6237;&#x540D;</li>
<li><code>-p&#xFF1A;</code>&#x5BC6;&#x7801;</li>
</ul>
<p>mysql&#x9ED8;&#x8BA4;&#x8FDE;&#x63A5;localhost&#x548C;3306&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x7701;&#x7565;-h&#x548C;-P:</p>
<p><code>mysql -u root -p</code></p>
<p>&#x5728;&#x547D;&#x4EE4;&#x884C;&#x4E2D;&#x8F93;&#x5165;&#x201C;help;&#x201D;&#x6216;&#x8005;&#x201C;\h&#x201D;&#xFF0C;&#x5C31;&#x4F1A;&#x663E;&#x793A;&#x51FA;MySQL&#x7684;&#x5E2E;&#x52A9;&#x4FE1;&#x606F;&#x3002;</p>
<table>
<thead>
<tr>
<th>&#x547D;&#x4EE4;</th>
<th>&#x7B80;&#x5199;</th>
<th>&#x5177;&#x4F53;&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td>?</td>
<td>\?</td>
<td>&#x663E;&#x793A;&#x5E2E;&#x52A9;&#x4FE1;&#x606F;</td>
</tr>
<tr>
<td>exit</td>
<td>\q</td>
<td>&#x9000;&#x51FA;MySQL</td>
</tr>
<tr>
<td>help</td>
<td>\h</td>
<td>&#x663E;&#x793A;&#x5E2E;&#x52A9;&#x4FE1;&#x606F;</td>
</tr>
<tr>
<td>quit</td>
<td>\q</td>
<td>&#x9000;&#x51FA;MySQL</td>
</tr>
<tr>
<td>status</td>
<td>\s</td>
<td>&#x83B7;&#x53D6;MySQL&#x670D;&#x52A1;&#x5668;&#x72B6;&#x6001;&#x4FE1;&#x606F;</td>
</tr>
<tr>
<td>use</td>
<td>\u</td>
<td>&#x7528;&#x6765;&#x9009;&#x62E9;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x4EE5;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x540D;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;</td>
</tr>
</tbody>
</table>
<h2 id="&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;"><a href="#&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;" class="headerlink" title="&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;"></a>&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B58;&#x50A8;&#x7ED3;&#x6784;</h2><ul>
<li>&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;</li>
<li>&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x53EF;&#x4EE5;&#x6709;&#x591A;&#x4E2A;&#x8868;<ul>
<li>&#x4E00;&#x4E2A;&#x8868;&#x4E00;&#x5B9A;&#x8981;&#x6709;&#x8868;&#x7ED3;&#x6784;</li>
<li>&#x6240;&#x6709;&#x7684;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5728;&#x8868;&#x4E2D;&#xFF0C;&#x6240;&#x4EE5;&#x6709;&#x4E86;&#x6570;&#x636E;&#x5E93;&#x4E4B;&#x540E;&#xFF0C;&#x8981;&#x5148;&#x8BBE;&#x8BA1;&#x4F60;&#x7684;&#x8868;&#x7ED3;&#x6784;</li>
</ul>
</li>
<li>&#x4E00;&#x5F20;&#x8868;&#x4E2D;&#x5B58;&#x50A8;&#x591A;&#x6761;&#x8BB0;&#x5F55;<ul>
<li>&#x8BB0;&#x5F55;&#x6309;&#x7167;&#x8868;&#x7ED3;&#x6784;&#x7684;&#x5F62;&#x5F0F;&#x8FDB;&#x884C;&#x5B58;&#x50A8;</li>
</ul>
</li>
</ul>
<h2 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x548C;&#x8868;"><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x548C;&#x8868;" class="headerlink" title="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x548C;&#x8868;"></a>&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;&#x548C;&#x8868;</h2><h3 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"></a>&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">CREATE</span> <span class="keyword">DATABASE</span> [<span class="keyword">IF</span> <span class="keyword">NOT</span> <span class="keyword">EXISTS</span>] db_name</div></pre></td></tr></table></figure>
<h3 id="&#x67E5;&#x770B;&#x6570;&#x636E;&#x5E93;"><a href="#&#x67E5;&#x770B;&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x67E5;&#x770B;&#x6570;&#x636E;&#x5E93;"></a>&#x67E5;&#x770B;&#x6570;&#x636E;&#x5E93;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SHOW</span> <span class="keyword">DATABASES</span>;</div></pre></td></tr></table></figure>
<p>&#x663E;&#x793A;&#x6570;&#x636E;&#x5E93;&#x521B;&#x5EFA;&#x8BED;&#x53E5;</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">DATABASE</span> db_name;</div></pre></td></tr></table></figure>
<h3 id="&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;"><a href="#&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;"></a>&#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">DROP</span> <span class="keyword">DATABASE</span> [<span class="keyword">IF</span> <span class="keyword">EXISTS</span>] db_name;</div></pre></td></tr></table></figure>
<h3 id="&#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;"><a href="#&#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;"></a>&#x9009;&#x62E9;&#x6570;&#x636E;&#x5E93;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">USE</span> db_name;</div></pre></td></tr></table></figure>
<h3 id="&#x67E5;&#x770B;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;"><a href="#&#x67E5;&#x770B;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x67E5;&#x770B;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;"></a>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x5E93;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">database</span>();</div></pre></td></tr></table></figure>
<h2 id="&#x6570;&#x636E;&#x7C7B;&#x578B;"><a href="#&#x6570;&#x636E;&#x7C7B;&#x578B;" class="headerlink" title="&#x6570;&#x636E;&#x7C7B;&#x578B;"></a>&#x6570;&#x636E;&#x7C7B;&#x578B;</h2><h3 id="&#x6574;&#x6570;&#x7C7B;&#x578B;"><a href="#&#x6574;&#x6570;&#x7C7B;&#x578B;" class="headerlink" title="&#x6574;&#x6570;&#x7C7B;&#x578B;"></a>&#x6574;&#x6570;&#x7C7B;&#x578B;</h3><table>
<thead>
<tr>
<th>&#x7C7B;&#x578B;</th>
<th>&#x5927;&#x5C0F;</th>
<th>&#x8303;&#x56F4;&#xFF08;&#x6709;&#x7B26;&#x53F7;&#xFF09;</th>
<th>&#x8303;&#x56F4;&#xFF08;&#x65E0;&#x7B26;&#x53F7;&#xFF09;</th>
<th>&#x7528;&#x9014;</th>
</tr>
</thead>
<tbody>
<tr>
<td>TINYINT</td>
<td>1 &#x5B57;&#x8282;</td>
<td>(-128&#xFF0C;127)</td>
<td>(0&#xFF0C;255)</td>
<td>&#x5C0F;&#x6574;&#x6570;&#x503C;</td>
</tr>
<tr>
<td>SMALLINT</td>
<td>2 &#x5B57;&#x8282;</td>
<td>(-32 768&#xFF0C;32 767)</td>
<td>(0&#xFF0C;65 535)</td>
<td>&#x5927;&#x6574;&#x6570;&#x503C;</td>
</tr>
<tr>
<td>MEDIUMINT</td>
<td>3 &#x5B57;&#x8282;</td>
<td>(-8 388 608&#xFF0C;8 388 607)</td>
<td>(0&#xFF0C;16 777 215)</td>
<td>&#x5927;&#x6574;&#x6570;&#x503C;</td>
</tr>
<tr>
<td>INT&#x6216;INTEGER</td>
<td>4 &#x5B57;&#x8282;</td>
<td>(-2 147 483 648&#xFF0C;2 147 483 647)</td>
<td>(0&#xFF0C;4 294 967 295)</td>
<td>&#x5927;&#x6574;&#x6570;&#x503C;</td>
</tr>
<tr>
<td>&#x2026;</td>
<td>&#x2026;</td>
<td>&#x2026;</td>
<td>&#x2026;</td>
<td>&#x2026;</td>
</tr>
</tbody>
</table>
<h3 id="&#x6D6E;&#x70B9;&#x578B;"><a href="#&#x6D6E;&#x70B9;&#x578B;" class="headerlink" title="&#x6D6E;&#x70B9;&#x578B;"></a>&#x6D6E;&#x70B9;&#x578B;</h3><p>&#x5728;MySQL&#x6570;&#x636E;&#x5E93;&#x4E2D;&#xFF0C;&#x5B58;&#x50A8;&#x5C0F;&#x6570;&#x90FD;&#x662F;&#x4F7F;&#x7528;&#x6D6E;&#x70B9;&#x6570;&#x548C;&#x5B9A;&#x70B9;&#x6570;&#x6765;&#x8868;&#x793A;&#x7684;&#x3002;</p>
<p>&#x6D6E;&#x70B9;&#x6570;&#x6709;&#x4E24;&#x79CD;&#xFF1A;</p>
<ul>
<li>&#x5355;&#x7CBE;&#x5EA6;&#x6D6E;&#x70B9;&#x6570;&#xFF08;FLOAT&#xFF09;</li>
<li>&#x53CC;&#x7CBE;&#x5EA6;&#x6D6E;&#x70B9;&#x6570;(DOUBLE)</li>
</ul>
<p>&#x5B9A;&#x70B9;&#x6570;&#xFF08;DECIMAL&#xFF09;[&#x2C8;des&#x26A;ml]</p>
<h3 id="&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#x7C7B;&#x578B;"><a href="#&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#x7C7B;&#x578B;" class="headerlink" title="&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#x7C7B;&#x578B;"></a>&#x65F6;&#x95F4;&#x548C;&#x65E5;&#x671F;&#x7C7B;&#x578B;</h3><table>
<thead>
<tr>
<th>&#x7C7B;&#x578B;</th>
<th>&#x5927;&#x5C0F;(&#x5B57;&#x8282;)</th>
<th>&#x8303;&#x56F4;</th>
<th>&#x683C;&#x5F0F;</th>
<th>&#x7528;&#x9014;</th>
</tr>
</thead>
<tbody>
<tr>
<td>DATE</td>
<td>3</td>
<td>1000-01-01/9999-12-31</td>
<td>YYY-MM-DD</td>
<td>&#x65E5;&#x671F;&#x503C;</td>
</tr>
<tr>
<td>TIME</td>
<td>3</td>
<td>&#x2018;-838:59:59&#x2019;/&#x2018;838:59:59&#x2019;</td>
<td>H:MM:SS</td>
<td>&#x65F6;&#x95F4;&#x503C;&#x6216;&#x6301;&#x7EED;&#x65F6;&#x95F4;</td>
</tr>
<tr>
<td>YEAR</td>
<td>1</td>
<td>1901/2155</td>
<td>YYY</td>
<td>&#x5E74;&#x4EFD;&#x503C;</td>
</tr>
<tr>
<td>DATETIME</td>
<td>8</td>
<td>1000-01-01 00:00:00/9999-12-31 23:59:59</td>
<td>YYY-MM-DD HH:MM:SS</td>
<td>&#x6DF7;&#x5408;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;&#x503C;</td>
</tr>
<tr>
<td>TIMESTAMP</td>
<td>8</td>
<td>1970-01-01 00:00:00/2037 &#x5E74;&#x67D0;&#x65F6;</td>
<td>YYYMMDD HHMMSS</td>
<td>&#x6DF7;&#x5408;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;&#x503C;&#xFF0C;&#x65F6;&#x95F4;&#x6233;</td>
</tr>
</tbody>
</table>
<p>&#x5982;&#x679C;&#x63D2;&#x5165;&#x7684;&#x6570;&#x503C;&#x4E0D;&#x5408;&#x6CD5;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x81EA;&#x52A8;&#x5C06;&#x5BF9;&#x5E94;&#x7684;&#x96F6;&#x503C;&#x63D2;&#x5165;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x3002;</p>
<ul>
<li>YEAR</li>
</ul>
<p>&#x4F7F;&#x7528;4&#x4F4D;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x6570;&#x5B57;&#x8868;&#x793A;&#xFF0C;&#x8303;&#x56F4;&#x4E3A;&#x2018;1901&#x2018; ~ &#x2018;2155&#x2019;&#x6216;1901~2155</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x8F93;&#x5165;&#x2018;2016&#x2018;&#x6216;&#x8005;2016&#xFF0C;&#x63D2;&#x5165;&#x5230;&#x6570;&#x636E;&#x5E93;&#x7684;&#x503C;&#x5747;&#x4E3A;2016</p>
<ul>
<li>DATE</li>
</ul>
<p>DATE&#x7C7B;&#x578B;&#x7528;&#x6765;&#x8868;&#x793A;&#x65E5;&#x671F;&#x503C;&#xFF0C;&#x4E0D;&#x5305;&#x542B;&#x65F6;&#x95F4;&#x90E8;&#x5206;&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x201C;YYYY-MM-DD&#x201C;&#x6216;&#x2018;YYYYMMDD&#x2018;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x8F93;&#x5165;&#x2018;2016-10-01&#x2018;&#x6216;&#x2019;20161001&#x2018;&#x63D2;&#x5165;&#x5230;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65E5;&#x671F;&#x90FD;&#x662F;2016-10-01</p>
<ul>
<li>TIME</li>
</ul>
<p>TIME&#x7C7B;&#x578B;&#x7528;&#x4E8E;&#x8868;&#x793A;&#x65F6;&#x95F4;&#x503C;&#xFF0C;&#x5B83;&#x7684;&#x663E;&#x793A;&#x5F62;&#x5F0F;&#x4E00;&#x822C;&#x4E3A;HH:MM:SS,&#x5176;&#x4E2D;HH &#x8868;&#x793A;&#x5C0F;&#x65F6;&#xFF0C;MM&#x8868;&#x793A;&#x5206;&#xFF0C;SS&#x8868;&#x793A;&#x79D2;</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#x7684;&#x503C;&#xFF1A;</p>
<ol>
<li>&#x4EE5;&#x201C;D HH&#xFF1A;MM&#xFF1A;SS&#x201C;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x8868;&#x793A;&#x3002;&#x5176;&#x4E2D;&#xFF0C;D&#x8868;&#x793A;&#x65E5;&#xFF0C;&#x53EF;&#x4EE5;&#x53D6;0-34&#x4E4B;&#x95F4;&#x7684;&#x503C;&#xFF0C;&#x63D2;&#x5165;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5C0F;&#x65F6;&#x7684;&#x503C;&#x7B49;&#x4E8E;&#xFF08;D*24+HH&#xFF09;</li>
</ol>
<ul>
<li>&#x4F8B;&#x5982;&#xFF0C;&#x8F93;&#x5165;&#x2018;2 11:30:50&#x2018;&#xFF0C;&#x63D2;&#x5165;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65E5;&#x671F;&#x4E3A; 59:30:50</li>
</ul>
<ol>
<li>&#x4EE5;&#x2018;HHMMSS&#x2018;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x6216;&#x8005;HHMMSS&#x6570;&#x5B57;&#x683C;&#x5F0F;&#x8868;&#x793A;</li>
</ol>
<ul>
<li>&#x4F8B;&#x5982;&#xFF1A;&#x8F93;&#x5165;&#x2018;345454&#x2018;&#x6216;345454&#xFF0C;&#x63D2;&#x5165;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65E5;&#x671F;&#x4E3A;34:54:54</li>
</ul>
<ol>
<li>&#x4F7F;&#x7528;CURRENT_TIME&#x6216;NOW()&#x8F93;&#x5165;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x65F6;&#x95F4;</li>
</ol>
<ul>
<li>DATETIME</li>
</ul>
<p>&#x6307;&#x5B9A;DATETIME&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF1A;</p>
<ol>
<li>&#x4EE5;&#x2018;YYYY-MM-DD HH:MM:SS&#x2018;&#x6216;&#x8005;&#x2019;YYYYMMDDHHMMSS&#x2018;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x6570;&#x5B57;&#x90FD;&#x53EF;&#x4EE5;&#x3002;</li>
<li>&#x4F7F;&#x7528;NOW&#x6765;&#x8F93;&#x5165;&#x5F53;&#x524D;&#x7CFB;&#x7EDF;&#x7684;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;</li>
</ol>
<ul>
<li>TIMESTAMP</li>
</ul>
<p>TIMESTAMP&#x7C7B;&#x578B;&#x663E;&#x793A;&#x5F62;&#x5F0F;&#x548C;DATETIME&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x53D6;&#x503C;&#x8303;&#x56F4;&#x6BD4;DATETIME&#x5C0F;&#x3002;</p>
<ol>
<li>&#x8F93;&#x5165;CURRENT_TIMESTAMP&#x8F93;&#x5165;&#x7CFB;&#x7EDF;&#x5F53;&#x524D;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;</li>
<li>&#x8F93;&#x5165;NULL&#x65F6;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x81EA;&#x52A8;&#x8F93;&#x5165;&#x5F53;&#x524D;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;</li>
<li>&#x65E0;&#x4EFB;&#x4F55;&#x8F93;&#x5165;&#x65F6;&#xFF0C;&#x7CFB;&#x7EDF;&#x4F1A;&#x8F93;&#x5165;&#x7CFB;&#x7EDF;&#x5F53;&#x524D;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;</li>
</ol>
<p>&#x6807;&#x8BB0;&#x3002;&#x5229;&#x7528;&#x5B83;&#x53EF;&#x4EE5;&#x4FDD;&#x5B58;&#x8868;&#x4E2D;&#x67D0;&#x6761;&#x8BB0;&#x5F55;&#x7684;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x3002;&#x81EA;&#x52A8;&#x7EF4;&#x62A4;&#x3002;</p>
<h3 id="CHAR&#x548C;VARCHAR"><a href="#CHAR&#x548C;VARCHAR" class="headerlink" title="CHAR&#x548C;VARCHAR"></a>CHAR&#x548C;VARCHAR</h3><table>
<thead>
<tr>
<th>&#x63D2;&#x5165;&#x503C;</th>
<th>CHAR(4)</th>
<th>&#x5B58;&#x50A8;&#x9700;&#x6C42;</th>
<th>VARCHAR(4)</th>
<th>&#x5B58;&#x50A8;&#x9700;&#x6C42;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x2018;&#x2019;</td>
<td>&#x2018;&#x2019;</td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
<td>&#x2018;&#x2019;</td>
<td>1&#x4E2A;&#x5B57;&#x8282;</td>
</tr>
<tr>
<td>&#x2018;ab&#x2019;</td>
<td>&#x2018;ab&#x2019;</td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
<td>&#x2018;ab&#x2019;</td>
<td>3&#x4E2A;&#x5B57;&#x8282;</td>
</tr>
<tr>
<td>&#x2018;abc&#x2019;</td>
<td></td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
<td>&#x2018;abc&#x2019;</td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
</tr>
<tr>
<td>&#x2018;abcd&#x2019;</td>
<td></td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
<td>&#x2018;abcd&#x2019;</td>
<td>5&#x4E2A;&#x5B57;&#x8282;</td>
</tr>
<tr>
<td>&#x2018;abcde&#x2019;</td>
<td>&#x2018;abcd&#x2019;</td>
<td>4&#x4E2A;&#x5B57;&#x8282;</td>
<td>&#x2018;abcd&#x2019;</td>
<td>5&#x4E2A;&#x5B57;&#x8282;</td>
</tr>
</tbody>
</table>
<p>&#x5F53;&#x6570;&#x636E;&#x4E3A;CHAR(4)&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4E0D;&#x7BA1;&#x63D2;&#x5165;&#x503C;&#x7684;&#x957F;&#x5EA6;&#x662F;&#x591A;&#x5C11;&#xFF0C;&#x6240;&#x5360;&#x7528;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#x90FD;&#x662F;4&#x4E2A;&#x5B57;&#x8282;&#x3002;&#x800C;VARCHAR&#xFF08;4&#xFF09;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x6240;&#x5360;&#x7528;&#x7684;&#x5B57;&#x8282;&#x6570;&#x4E3A;&#x5B9E;&#x9645;&#x957F;&#x5EA6;&#x52A0;1.</p>
<p>&#x603B;&#x7ED3;&#xFF1A;</p>
<ul>
<li>&#x5B57;&#x7B26;&#x957F;&#x5EA6;&#x4E0D;&#x56FA;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#x4F7F;&#x7528;VARCHAR &#x67E5;&#x8BE2;&#x7684;&#x65F6;&#x5019;&#x8981;&#x8BA1;&#x7B97;&#x5B57;&#x8282;&#x7684;&#x957F;&#x5EA6;</li>
<li><p>&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;&#x56FA;&#x5B9A;&#x7684;&#x4F7F;&#x7528;CHAR  &#x67E5;&#x8BE2;&#x901F;&#x5EA6;&#x5FEB;&#x3002;</p>
</li>
<li><p>VARCAHR&#x6BD4;CHAR&#x7701;&#x7A7A;&#x95F4;</p>
</li>
<li>CHAR&#x6BD4;VARCHAR&#x7701;&#x65F6;&#x95F4;</li>
</ul>
<p>TEXT&#x7C7B;&#x578B;</p>
<p>&#x8868;&#x793A;&#x5927;&#x6587;&#x672C;&#x6570;&#x636E;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x6587;&#x7AE0;&#x5185;&#x5BB9;&#x3001;&#x8BC4;&#x8BBA;&#x7B49; </p>
<h2 id="&#x57FA;&#x672C;&#x64CD;&#x4F5C;-1"><a href="#&#x57FA;&#x672C;&#x64CD;&#x4F5C;-1" class="headerlink" title="&#x57FA;&#x672C;&#x64CD;&#x4F5C;"></a>&#x57FA;&#x672C;&#x64CD;&#x4F5C;</h2><h3 id="&#x67E5;&#x770B;&#x8868;&#x7ED3;&#x6784;"><a href="#&#x67E5;&#x770B;&#x8868;&#x7ED3;&#x6784;" class="headerlink" title="&#x67E5;&#x770B;&#x8868;&#x7ED3;&#x6784;"></a>&#x67E5;&#x770B;&#x8868;&#x7ED3;&#x6784;</h3><p>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x8868;&#xFF1A; <code>show tables;</code></p>
<p>&#x67E5;&#x770B;&#x8868;&#x7ED3;&#x6784;: <code>desc table_name;</code></p>
<p>&#x67E5;&#x770B;&#x5EFA;&#x8868;&#x8BED;&#x53E5;: <code>show create table table_name;</code></p>
<h3 id="&#x4FEE;&#x6539;&#x6570;&#x636E;&#x8868;"><a href="#&#x4FEE;&#x6539;&#x6570;&#x636E;&#x8868;" class="headerlink" title="&#x4FEE;&#x6539;&#x6570;&#x636E;&#x8868;"></a>&#x4FEE;&#x6539;&#x6570;&#x636E;&#x8868;</h3><p>&#x589E;&#x52A0;&#x5217;: <code>ALTER TABLE table_name ADD colum datatype;</code></p>
<p>&#x4FEE;&#x6539;&#x5217;: <code>ALTER TABLE table_name MODIFY colum datatype;</code></p>
<p>&#x5220;&#x9664;&#x5217;: <code>ALTER TABLE table_name DROP colum;</code></p>
<p>&#x4FEE;&#x6539;&#x8868;&#x540D;: <code>rename TABLE table_name to new_table_name;</code></p>
<p>&#x4FEE;&#x6539;&#x5217;&#x540D;: <code>ALTER TABLE table_name change colum_name new_colum_name datatype;</code></p>
<h3 id="&#x5220;&#x9664;&#x6570;&#x636E;&#x8868;"><a href="#&#x5220;&#x9664;&#x6570;&#x636E;&#x8868;" class="headerlink" title="&#x5220;&#x9664;&#x6570;&#x636E;&#x8868;"></a>&#x5220;&#x9664;&#x6570;&#x636E;&#x8868;</h3><p><code>DROP TABLE table_name;</code></p>
<h3 id="&#x8868;&#x7684;&#x7EA6;&#x675F;"><a href="#&#x8868;&#x7684;&#x7EA6;&#x675F;" class="headerlink" title="&#x8868;&#x7684;&#x7EA6;&#x675F;"></a>&#x8868;&#x7684;&#x7EA6;&#x675F;</h3><table>
<thead>
<tr>
<th>&#x7EA6;&#x675F;&#x6761;&#x4EF6;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>PRIMARY KEY</td>
<td>&#x4E3B;&#x952E;&#x7EA6;&#x675F;&#xFF0C;&#x7528;&#x4E8E;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x5BF9;&#x5E94;&#x7684;&#x8BB0;&#x5F55;</td>
</tr>
<tr>
<td>FOREIGN KEY</td>
<td>&#x5916;&#x952E;&#x7EA6;&#x675F;</td>
</tr>
<tr>
<td>NOT NULL</td>
<td>&#x975E;&#x7A7A;&#x7EA6;&#x675F;</td>
</tr>
<tr>
<td>UNIQUE</td>
<td>&#x552F;&#x4E00;&#x6027;&#x7EA6;&#x675F;</td>
</tr>
<tr>
<td>DEFAULT</td>
<td>&#x9ED8;&#x8BA4;&#x503C;&#x7EA6;&#x675F;&#xFF0C;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x5B57;&#x6BB5;&#x7684;&#x9ED8;&#x8BA4;&#x503C;</td>
</tr>
</tbody>
</table>
<p>&#x8868;&#x7684;&#x7EA6;&#x675F;&#x6761;&#x4EF6;&#x90FD;&#x662F;&#x9488;&#x5BF9;&#x8868;&#x4E2D;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x9650;&#x5236;&#xFF0C;&#x4ECE;&#x800C;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x8868;&#x4E2D;&#x6570;&#x636E;&#x7684;&#x6B63;&#x786E;&#x6027;&#x548C;&#x552F;&#x4E00;&#x6027;&#x3002;</p>
<h4 id="&#x4E3B;&#x952E;&#x7EA6;&#x675F;"><a href="#&#x4E3B;&#x952E;&#x7EA6;&#x675F;" class="headerlink" title="&#x4E3B;&#x952E;&#x7EA6;&#x675F;"></a>&#x4E3B;&#x952E;&#x7EA6;&#x675F;</h4><p>&#x6BCF;&#x4E2A;&#x6570;&#x636E;&#x8868;&#x4E2D;&#x6700;&#x591A;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x4E3B;&#x952E;&#x7EA6;&#x675F;&#xFF0C;&#x5B9A;&#x4E49;&#x4E3A;PRIMARY KEY &#x7684;&#x5B57;&#x6BB5;&#x4E0D;&#x80FD;&#x6709;&#x91CD;&#x590D;&#x503C;&#x4E14;&#x4E0D;&#x80FD;&#x4E3A;NULL&#x503C;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x975E;&#x7A7A;&#x800C;&#x4E14;&#x552F;&#x4E00;</p>
<p>&#x8BED;&#x6CD5;&#xFF1A;<code>&#x5B57;&#x6BB5;&#x540D; &#x6570;&#x636E;&#x7C7B;&#x578B; PRIMARY KEY</code></p>
<h4 id="&#x975E;&#x7A7A;&#x7EA6;&#x675F;"><a href="#&#x975E;&#x7A7A;&#x7EA6;&#x675F;" class="headerlink" title="&#x975E;&#x7A7A;&#x7EA6;&#x675F;"></a>&#x975E;&#x7A7A;&#x7EA6;&#x675F;</h4><p>&#x975E;&#x7A7A;&#x7EA6;&#x675F;&#x6307;&#x7684;&#x662F;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#x4E0D;&#x80FD;&#x4E3A;NULL&#xFF0C;&#x5728;MySQL&#x4E2D;&#xFF0C;&#x975E;&#x7A7A;&#x7EA6;&#x675F;&#x662F;&#x901A;&#x8FC7;NOT NULL &#x5B9A;&#x4E49;&#x7684;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#xFF1A;<code>&#x5B57;&#x6BB5;&#x540D; &#x6570;&#x636E;&#x7C7B;&#x578B; NOT NULL;</code></p>
<h4 id="&#x552F;&#x4E00;&#x7EA6;&#x675F;"><a href="#&#x552F;&#x4E00;&#x7EA6;&#x675F;" class="headerlink" title="&#x552F;&#x4E00;&#x7EA6;&#x675F;"></a>&#x552F;&#x4E00;&#x7EA6;&#x675F;</h4><p>&#x552F;&#x4E00;&#x7EA6;&#x675F;&#x7528;&#x4E8E;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x8868;&#x4E2D;&#x5B57;&#x6BB5;&#x7684;&#x552F;&#x4E00;&#x6027;&#xFF0C;&#x5373;&#x8868;&#x4E2D;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#x4E0D;&#x80FD;&#x91CD;&#x590D;&#x51FA;&#x73B0;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#xFF1A;<code>&#x5B57;&#x6BB5;&#x540D; &#x6570;&#x636E;&#x7C7B;&#x578B; UNIQUE;</code></p>
<h4 id="&#x9ED8;&#x8BA4;&#x7EA6;&#x675F;"><a href="#&#x9ED8;&#x8BA4;&#x7EA6;&#x675F;" class="headerlink" title="&#x9ED8;&#x8BA4;&#x7EA6;&#x675F;"></a>&#x9ED8;&#x8BA4;&#x7EA6;&#x675F;</h4><p>&#x9ED8;&#x8BA4;&#x7EA6;&#x675F;&#x7528;&#x4E8E;&#x7ED9;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;&#x6307;&#x5B9A;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5373;&#x5F53;&#x5728;&#x8868;&#x4E2D;&#x63D2;&#x5165;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7ED9;&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x8D4B;&#x503C;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7CFB;&#x7EDF;&#x4F1A;&#x4E3A;&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x63D2;&#x5165;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#xFF1A;<code>&#x5B57;&#x6BB5;&#x540D; &#x6570;&#x636E;&#x7C7B;&#x578B; DEFAULT &#x9ED8;&#x8BA4;&#x503C;;</code></p>
<h3 id="&#x8BBE;&#x7F6E;&#x8868;&#x7684;&#x5B57;&#x6BB5;&#x503C;&#x81EA;&#x52A8;&#x589E;&#x52A0;"><a href="#&#x8BBE;&#x7F6E;&#x8868;&#x7684;&#x5B57;&#x6BB5;&#x503C;&#x81EA;&#x52A8;&#x589E;&#x52A0;" class="headerlink" title="&#x8BBE;&#x7F6E;&#x8868;&#x7684;&#x5B57;&#x6BB5;&#x503C;&#x81EA;&#x52A8;&#x589E;&#x52A0;"></a>&#x8BBE;&#x7F6E;&#x8868;&#x7684;&#x5B57;&#x6BB5;&#x503C;&#x81EA;&#x52A8;&#x589E;&#x52A0;</h3><p>&#x5982;&#x679C;&#x60F3;&#x4E3A;&#x8868;&#x4E2D;&#x63D2;&#x5165;&#x7684;&#x65B0;&#x7EAA;&#x5F55;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x552F;&#x4E00;ID&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;AUTO_INCREMENT&#x5B9E;&#x73B0;</p>
<p>&#x8BED;&#x6CD5;&#xFF1A;<code>&#x5B57;&#x6BB5;&#x540D; &#x6570;&#x636E;&#x7C7B;&#x578B; AUTO_INCREMENT;</code></p>
<h2 id="&#x6DFB;&#x52A0;&#x3001;&#x66F4;&#x65B0;&#x4E0E;&#x5220;&#x9664;&#x6570;&#x636E;"><a href="#&#x6DFB;&#x52A0;&#x3001;&#x66F4;&#x65B0;&#x4E0E;&#x5220;&#x9664;&#x6570;&#x636E;" class="headerlink" title="&#x6DFB;&#x52A0;&#x3001;&#x66F4;&#x65B0;&#x4E0E;&#x5220;&#x9664;&#x6570;&#x636E;"></a>&#x6DFB;&#x52A0;&#x3001;&#x66F4;&#x65B0;&#x4E0E;&#x5220;&#x9664;&#x6570;&#x636E;</h2><h3 id="&#x6DFB;&#x52A0;&#x6570;&#x636E;"><a href="#&#x6DFB;&#x52A0;&#x6570;&#x636E;" class="headerlink" title="&#x6DFB;&#x52A0;&#x6570;&#x636E;"></a>&#x6DFB;&#x52A0;&#x6570;&#x636E;</h3><h4 id="&#x4E3A;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x6570;&#x636E;"><a href="#&#x4E3A;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x6570;&#x636E;" class="headerlink" title="&#x4E3A;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x6570;&#x636E;"></a>&#x4E3A;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x6570;&#x636E;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> &#x8868;&#x540D;</div><div class="line"><span class="keyword">VALUES</span>(&#x5217;<span class="number">1</span>&#x503C;,&#x5217;<span class="number">2</span>&#x503C;,...)</div></pre></td></tr></table></figure>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong></p>
<ul>
<li>values&#x4E2D;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x4E0E;&#x8868;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#x3002;</li>
<li>&#x63D2;&#x5165;&#x7684;&#x6570;&#x636E;&#x5E94;&#x4E0E;&#x5B57;&#x6BB5;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x76F8;&#x540C;</li>
<li>&#x6570;&#x636E;&#x7684;&#x5927;&#x5C0F;&#x5E94;&#x8BE5;&#x5728;&#x5217;&#x7684;&#x89C4;&#x5B9A;&#x8303;&#x56F4;&#x5185;&#xFF0C;&#x4F8B;&#x5982;&#x4E0D;&#x80FD;&#x5C06;&#x4E00;&#x4E2A;&#x957F;&#x5EA6;&#x4E3A;80&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x63D2;&#x5165;&#x5230;&#x957F;&#x5EA6;&#x4E3A;40&#x4E2A;&#x5217;&#x4E2D;</li>
<li>&#x5B57;&#x7B26;&#x548C;&#x65E5;&#x671F;&#x578B;&#x6570;&#x636E;&#x5E94;&#x8BE5;&#x5305;&#x542B;&#x5728;&#x5355;&#x5F15;&#x53F7;&#x4E2D;</li>
<li>&#x5982;&#x679C;&#x8981;&#x63D2;&#x5165;&#x4E00;&#x4E2A;&#x7A7A;&#x503C;&#xFF0C;&#x4E0D;&#x6307;&#x5B9A;&#x6216;&#x8005;&#x4F7F;&#x7528;NULL</li>
</ul>
<h4 id="&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x6DFB;&#x52A0;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x6DFB;&#x52A0;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x6DFB;&#x52A0;&#x6570;&#x636E;&#xFF1A;"></a>&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x6DFB;&#x52A0;&#x6570;&#x636E;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> &#x8868;&#x540D;(&#x5217;<span class="number">1</span>&#x540D;, &#x5217;<span class="number">2</span>&#x540D;,...)</div><div class="line"><span class="keyword">VALUES</span>(&#x5217;<span class="number">1</span>&#x503C;, &#x5217;<span class="number">2</span>&#x503C;,...)</div></pre></td></tr></table></figure>
<p><strong>&#x6CE8;&#x610F;: values&#x4E2D;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x4E0E;&#x5217;&#x58F0;&#x660E;&#x4E2D;&#x7684;&#x5217;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;</strong></p>
<h4 id="&#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x591A;&#x6761;&#x8BB0;&#x5F55;"><a href="#&#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x591A;&#x6761;&#x8BB0;&#x5F55;" class="headerlink" title="&#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x591A;&#x6761;&#x8BB0;&#x5F55;"></a>&#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x591A;&#x6761;&#x8BB0;&#x5F55;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee</div><div class="line"><span class="keyword">VALUES</span> (value1,value2,value3...),</div><div class="line">        (value1,value2,value3...),</div><div class="line">        (value1,value2,value3),</div><div class="line">        ...;</div></pre></td></tr></table></figure>
<h3 id="&#x66F4;&#x65B0;&#x5168;&#x90E8;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x66F4;&#x65B0;&#x5168;&#x90E8;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x66F4;&#x65B0;&#x5168;&#x90E8;&#x6570;&#x636E;&#xFF1A;"></a>&#x66F4;&#x65B0;&#x5168;&#x90E8;&#x6570;&#x636E;&#xFF1A;</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">UPDATE</span> &#x8868;&#x540D;</div><div class="line"><span class="keyword">SET</span> &#x5217;&#x540D;=&#x503C;, &#x5217;&#x540D;=&#x503C;[,&#x5217;&#x540D;=&#x503C;]</div></pre></td></tr></table></figure>
<h4 id="&#x6309;&#x6761;&#x4EF6;&#x66F4;&#x65B0;&#xFF1A;"><a href="#&#x6309;&#x6761;&#x4EF6;&#x66F4;&#x65B0;&#xFF1A;" class="headerlink" title="&#x6309;&#x6761;&#x4EF6;&#x66F4;&#x65B0;&#xFF1A;"></a>&#x6309;&#x6761;&#x4EF6;&#x66F4;&#x65B0;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">UPDATE</span> &#x8868;&#x540D;</div><div class="line"><span class="keyword">SET</span> &#x5217;&#x540D;=&#x503C;, &#x5217;&#x540D;=&#x503C;[,&#x5217;&#x540D;=&#x503C;]</div><div class="line"><span class="keyword">WHERE</span> &#x6761;&#x4EF6;;</div></pre></td></tr></table></figure>
<p>&#x603B;&#x7ED3;&#xFF1A;</p>
<ul>
<li>UPDATE&#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x7528;&#x65B0;&#x503C;&#x66F4;&#x65B0;&#x539F;&#x6709;&#x8868;&#x4E2D;&#x884C;&#x7684;&#x5217;&#x3002;</li>
<li>SET&#x5B57;&#x53E5;&#x6307;&#x5B9A;&#x8981;&#x4FEE;&#x6539;&#x54EA;&#x4E9B;&#x5217;&#x548C;&#x8981;&#x7ED9;&#x4E0E;&#x54EA;&#x4E9B;&#x503C;</li>
<li>WHERE&#x9700;&#x8981;&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#xFF0C;&#x8868;&#x793A;&#x8981;&#x66F4;&#x65B0;&#x7B26;&#x53F7;&#x8BE5;&#x6761;&#x4EF6;&#x7684;&#x884C;&#xFF0C;&#x6CA1;&#x6709;WHERE&#x5B57;&#x53E5;&#xFF0C;&#x5219;&#x66F4;&#x65B0;&#x6240;&#x6709;&#x884C;</li>
</ul>
<h4 id="&#x6761;&#x4EF6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;"><a href="#&#x6761;&#x4EF6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;" class="headerlink" title="&#x6761;&#x4EF6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;"></a>&#x6761;&#x4EF6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- &#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26; &gt; &lt; &lt;= &gt;= = &lt;&gt;  &#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;&#x3001;&#x5927;&#x4E8E;(&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;)&#x3001;&#x4E0D;&#x7B49;&#x4E8E;</span></div><div class="line">BETWEEN&#x2026;AND <span class="comment">-- &#x663E;&#x793A;&#x5728;&#x67D0;&#x4E00;&#x533A;&#x95F4;&#x7684;&#x503C;</span></div><div class="line">IN(<span class="keyword">set</span>) <span class="comment">-- &#x663E;&#x793A;&#x5728;in&#x5217;&#x8868;&#x4E2D;&#x7684;&#x503C;&#xFF0C;&#x4F8B;&#xFF1A;in(100,200)</span></div><div class="line"><span class="keyword">LIKE</span> <span class="comment">-- &#x2018;&#x5F20;pattern&#x2019; &#x6A21;&#x7CCA;&#x67E5;&#x8BE2;%</span></div><div class="line"><span class="keyword">IS</span> <span class="literal">NULL</span> <span class="comment">-- &#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x7A7A;</span></div><div class="line"></div><div class="line"><span class="comment">-- &#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26; AND &#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x540C;&#x65F6;&#x6210;&#x7ACB;</span></div><div class="line"><span class="keyword">OR</span>  &#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x4EFB;&#x4E00;&#x6210;&#x7ACB;</div><div class="line"><span class="keyword">NOT</span> &#x4E0D;&#x6210;&#x7ACB;&#xFF0C;&#x4F8B;&#xFF1A;<span class="keyword">WHERE</span> <span class="keyword">NOT</span>(salary&gt;<span class="number">100</span>)</div></pre></td></tr></table></figure>
<h3 id="&#x5220;&#x9664;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x5220;&#x9664;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x5220;&#x9664;&#x6570;&#x636E;&#xFF1A;"></a>&#x5220;&#x9664;&#x6570;&#x636E;&#xFF1A;</h3><h4 id="&#x5220;&#x9664;&#x5168;&#x90E8;&#x6570;&#x636E;"><a href="#&#x5220;&#x9664;&#x5168;&#x90E8;&#x6570;&#x636E;" class="headerlink" title="&#x5220;&#x9664;&#x5168;&#x90E8;&#x6570;&#x636E;"></a>&#x5220;&#x9664;&#x5168;&#x90E8;&#x6570;&#x636E;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> &#x8868;&#x540D;;</div></pre></td></tr></table></figure>
<h4 id="&#x6839;&#x636E;&#x6761;&#x4EF6;&#x5220;&#x9664;&#xFF1A;"><a href="#&#x6839;&#x636E;&#x6761;&#x4EF6;&#x5220;&#x9664;&#xFF1A;" class="headerlink" title="&#x6839;&#x636E;&#x6761;&#x4EF6;&#x5220;&#x9664;&#xFF1A;"></a>&#x6839;&#x636E;&#x6761;&#x4EF6;&#x5220;&#x9664;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> &#x8868;&#x540D;</div><div class="line"><span class="keyword">WHERE</span> &#x6761;&#x4EF6;;</div></pre></td></tr></table></figure>
<h4 id="&#x521D;&#x59CB;&#x5316;"><a href="#&#x521D;&#x59CB;&#x5316;" class="headerlink" title="&#x521D;&#x59CB;&#x5316;"></a>&#x521D;&#x59CB;&#x5316;</h4><p>runcate&#x521D;&#x59CB;&#x5316;&#x6570;&#x636E;&#x8868;</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">truncate</span> table_name;</div></pre></td></tr></table></figure>
<p><strong>truncate&#x548C;delete&#x7684;&#x533A;&#x522B;&#xFF1A;</strong></p>
<ul>
<li>delete&#x4F1A;&#x4E00;&#x6761;&#x4E00;&#x6761;&#x7684;&#x5220;</li>
<li>truncate&#x5148;&#x6467;&#x6BC1;&#x6574;&#x5F20;&#x8868;&#xFF0C;&#x518D;&#x521B;&#x5EFA;&#x4E00;&#x5F20;&#x548C;&#x539F;&#x6765;&#x7684;&#x8868;&#x7ED3;&#x6784;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#x7684;&#x8868;</li>
<li>&#x62FF;&#x62C6;&#x8FC1;&#x4E3E;&#x4F8B;&#x5B50;</li>
<li>truncate&#x5728;&#x6548;&#x7387;&#x4E0A;&#x6BD4;delete&#x9AD8;</li>
<li>truncate&#x53EA;&#x80FD;&#x5220;&#x9664;&#x6574;&#x8868;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x683C;&#x5F0F;&#x5316;&#x3002;</li>
<li>truncate&#x4F1A;&#x628A;&#x81EA;&#x589E;id&#x622A;&#x65AD;&#x6062;&#x590D;&#x4E3A;1</li>
</ul>
<p>&#x603B;&#x7ED3;&#xFF1A;</p>
<ul>
<li>&#x5982;&#x679C;&#x4E0D;&#x4F7F;&#x7528;WHERE&#x8BED;&#x53E5;&#xFF0C;&#x5C06;&#x5220;&#x9664;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li>DELETE&#x4E0D;&#x80FD;&#x5220;&#x9664;&#x67D0;&#x4E00;&#x5217;&#x7684;&#x503C;&#xFF0C;&#xFF08;&#x53EF;&#x4F7F;&#x7528;UPDATE&#xFF09;</li>
<li>&#x4F7F;&#x7528;DELETE&#x8BED;&#x53E5;&#x4EC5;&#x4EC5;&#x5220;&#x9664;&#x8BB0;&#x5F55;&#xFF0C;&#x4E0D;&#x5220;&#x9664;&#x8868;&#x672C;&#x8EAB;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x5220;&#x9664;&#x8868;&#xFF0C;&#x4F7F;&#x7528;DROP TABLE&#x8BED;&#x53E5;</li>
<li>&#x5220;&#x9664;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;truncate table_name&#x8BED;&#x53E5;</li>
</ul>
<h3 id="&#x5355;&#x8868;&#x67E5;&#x8BE2;"><a href="#&#x5355;&#x8868;&#x67E5;&#x8BE2;" class="headerlink" title="&#x5355;&#x8868;&#x67E5;&#x8BE2;"></a>&#x5355;&#x8868;&#x67E5;&#x8BE2;</h3><h4 id="&#x7B80;&#x5355;&#x67E5;&#x8BE2;"><a href="#&#x7B80;&#x5355;&#x67E5;&#x8BE2;" class="headerlink" title="&#x7B80;&#x5355;&#x67E5;&#x8BE2;"></a>&#x7B80;&#x5355;&#x67E5;&#x8BE2;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> [<span class="keyword">DISTINCT</span>] *|{colum1, colum2, colum3...} <span class="keyword">FROM</span> table_name;</div></pre></td></tr></table></figure>
<ul>
<li>SELECT&#x6307;&#x5B9A;&#x67E5;&#x8BE2;&#x54EA;&#x4E9B;&#x5217;&#x7684;&#x6570;&#x636E;</li>
<li>column&#x6307;&#x5B9A;&#x5217;&#x540D;</li>
<li><ul>
<li>&#x53F7;&#x8868;&#x793A;&#x67E5;&#x8BE2;&#x6240;&#x6709;&#x5217;</li>
</ul>
</li>
<li>FROM &#x6307;&#x5B9A;&#x67E5;&#x8BE2;&#x54EA;&#x79CD;&#x8868;</li>
<li>DISTINCT &#x53EF;&#x9009;&#xFF0C;&#x6307;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x65F6;&#xFF0C;&#x662F;&#x5426;&#x53BB;&#x9664;&#x91CD;&#x590D;&#x6570;&#x636E;</li>
</ul>
<h4 id="&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;"></a>&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> &#x8868;&#x540D;;</div></pre></td></tr></table></figure>
<h4 id="&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;"></a>&#x6309;&#x7167;&#x6307;&#x5B9A;&#x5217;&#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> &#x5217;&#x540D;,&#x5217;&#x540D;[,&#x5217;&#x540D;] <span class="keyword">FROM</span> &#x8868;&#x540D;;</div></pre></td></tr></table></figure>
<h4 id="&#x6839;&#x636E;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#xFF1A;"><a href="#&#x6839;&#x636E;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#xFF1A;" class="headerlink" title="&#x6839;&#x636E;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#xFF1A;"></a>&#x6839;&#x636E;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> * <span class="keyword">FROM</span> &#x8868;&#x540D;</div><div class="line"><span class="keyword">WHERE</span> &#x6761;&#x4EF6;;</div></pre></td></tr></table></figure>
<p>&#x5728;WHERE&#x5B57;&#x53E5;&#x4E2D;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x7684;&#x8FD0;&#x7B97;&#x7B26;</p>
<table>
<thead>
<tr>
<th>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</th>
<th>&gt; &lt; &lt;= &gt;= = &lt;&gt;</th>
<th>&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;&#x3001;&#x5927;&#x4E8E;(&#x5C0F;&#x4E8E;&#x7B49;&#x4E8E;)&#x3001;&#x4E0D;&#x7B49;&#x4E8E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>BETWEEN&#x2026;AND</td>
<td>&#x663E;&#x793A;&#x5728;&#x67D0;&#x4E00;&#x533A;&#x95F4;&#x7684;&#x503C;</td>
</tr>
<tr>
<td>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>IN(set)</td>
<td>&#x663E;&#x793A;&#x5728;in&#x5217;&#x8868;&#x4E2D;&#x7684;&#x503C;&#xFF0C;&#x4F8B;&#xFF1A;in(100,200)</td>
</tr>
<tr>
<td>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>LIKE</td>
<td>&#x2018;&#x5F20;pattern&#x2019; &#x6A21;&#x7CCA;&#x67E5;&#x8BE2;%</td>
</tr>
<tr>
<td>&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>IS NULL</td>
<td>&#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x7A7A;</td>
</tr>
<tr>
<td>&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>AND</td>
<td>&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x540C;&#x65F6;&#x6210;&#x7ACB;</td>
</tr>
<tr>
<td>&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>OR</td>
<td>&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x4EFB;&#x4E00;&#x6210;&#x7ACB;</td>
</tr>
<tr>
<td>&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;</td>
<td>NOT</td>
<td>&#x4E0D;&#x6210;&#x7ACB;&#xFF0C;&#x4F8B;&#xFF1A;WHERE NOT(salary&gt;100)</td>
</tr>
</tbody>
</table>
<p>LIKE&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;<code>%</code> &#x4EE3;&#x8868;&#x96F6;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x4EFB;&#x610F;&#x5B57;&#x7B26;&#xFF0C;<code>_</code>&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;<code>name LIKE &apos;_a%&apos;</code>;</p>
<h4 id="&#x591A;&#x8868;&#x67E5;&#x8BE2;&#xFF1A;"><a href="#&#x591A;&#x8868;&#x67E5;&#x8BE2;&#xFF1A;" class="headerlink" title="&#x591A;&#x8868;&#x67E5;&#x8BE2;&#xFF1A;"></a>&#x591A;&#x8868;&#x67E5;&#x8BE2;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- &#x591A;&#x8868;&#x67E5;&#x8BE2;</span></div><div class="line"><span class="comment">-- &#x627E;&#x5230;&#x8868; articles &#x4E2D; user_id &#x7B49;&#x4E8E; users &#x8868;&#x4E2D; id &#x7684;</span></div><div class="line"><span class="comment">-- &#x591A;&#x8868;&#x67E5;&#x8BE2;&#x53EF;&#x4EE5;&#x8D77;&#x522B;&#x540D;</span></div><div class="line"><span class="keyword">SELECT</span> a.id <span class="keyword">as</span> article_id, a.title, a.time</div><div class="line"><span class="keyword">FROM</span> articles <span class="keyword">as</span> a</div><div class="line"><span class="keyword">INSERT</span> <span class="keyword">JOIN</span> <span class="keyword">users</span> <span class="keyword">as</span> u</div><div class="line"><span class="keyword">ON</span> a.user_id=u.id</div></pre></td></tr></table></figure>
<h4 id="&#x67E5;&#x8BE2;&#x603B;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;"><a href="#&#x67E5;&#x8BE2;&#x603B;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;" class="headerlink" title="&#x67E5;&#x8BE2;&#x603B;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;"></a>&#x67E5;&#x8BE2;&#x603B;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">-- &#x67E5;&#x8BE2;&#x8868;&#x4E2D;&#x7684;&#x603B;&#x8BB0;&#x5F55;&#x6570;&#x636E;</span></div><div class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(<span class="keyword">id</span>) <span class="keyword">as</span> <span class="keyword">count</span> <span class="keyword">FROM</span> articles;</div></pre></td></tr></table></figure>
<h4 id="&#x805A;&#x5408;&#x51FD;&#x6570;"><a href="#&#x805A;&#x5408;&#x51FD;&#x6570;" class="headerlink" title="&#x805A;&#x5408;&#x51FD;&#x6570;"></a>&#x805A;&#x5408;&#x51FD;&#x6570;</h4><blockquote>
<p>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x7ECF;&#x5E38;&#x9700;&#x8981;&#x5BF9;&#x67D0;&#x4E9B;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x7EDF;&#x8BA1;&#xFF0C;&#x4F8B;&#x5982;&#x7EDF;&#x8BA1;&#x67D0;&#x4E2A;&#x5B57;&#x6BB5;&#x7684;&#x6700;&#x5927;&#x503C;&#xFF0C;&#x6700;&#x5C0F;&#x503C;&#xFF0C;&#x5E73;&#x5747;&#x503C;&#x7B49;&#xFF0C;&#x4E3A;&#x6B64;&#xFF0C;MySQL&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x3002;</p>
</blockquote>
<table>
<thead>
<tr>
<th>&#x51FD;&#x6570;&#x540D;&#x79F0;</th>
<th>&#x4F5C;&#x7528;</th>
</tr>
</thead>
<tbody>
<tr>
<td>COUNT()</td>
<td>&#x8FD4;&#x56DE;&#x67D0;&#x884C;&#x7684;&#x5217;&#x6570;</td>
</tr>
<tr>
<td>SUM()</td>
<td>&#x8FD4;&#x56DE;&#x67D0;&#x5217;&#x503C;&#x7684;&#x548C;</td>
</tr>
<tr>
<td>AVG()</td>
<td>&#x8FD4;&#x56DE;&#x67D0;&#x5217;&#x7684;&#x5E73;&#x5747;&#x503C;</td>
</tr>
<tr>
<td>MAX()</td>
<td>&#x8FD4;&#x56DE;&#x67D0;&#x5217;&#x503C;&#x7684;&#x6700;&#x5927;&#x503C;</td>
</tr>
<tr>
<td>MIN()</td>
<td>&#x8FD4;&#x56DE;&#x67D0;&#x5217;&#x7684;&#x6700;&#x5C0F;&#x503C;</td>
</tr>
</tbody>
</table>
<ul>
<li>COUNT&#xFF08;&#x5217;&#x540D;&#xFF09;&#x8FD4;&#x56DE;&#x67D0;&#x4E00;&#x5217;&#xFF0C;&#x884C;&#x7684;&#x603B;&#x6570;</li>
<li>COUNT&#xFF08;&#x5217;&#x540D;&#xFF09;&#x8FD4;&#x56DE;&#x67D0;&#x4E00;&#x5217;&#xFF0C;&#x884C;&#x7684;&#x603B;&#x6570;</li>
<li>SUM()&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x6EE1;&#x8DB3;WHERE&#x6761;&#x4EF6;&#x7684;&#x884C;&#x7684;&#x548C;</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">SUM</span>(&#x5217;&#x540D;) {, <span class="keyword">SUM</span>(&#x5217;&#x540D;)...} <span class="keyword">FROM</span> table_name</div><div class="line">  [<span class="keyword">WHERE</span> where_definition]</div></pre></td></tr></table></figure>
<p>&#x6CE8;&#x610F;&#xFF1A;SUM&#x4EC5;&#x5BF9;&#x6570;&#x503C;&#x8D77;&#x4F5C;&#x7528;&#xFF0C;&#x5426;&#x5219;&#x62A5;&#x9519;; &#x5BF9;&#x591A;&#x5217;&#x6C42;&#x548C;&#xFF0C;&#x201C;,&#x201D;&#x4E0D;&#x80FD;&#x5C11;&#x3002;</p>
<ul>
<li>MAX()/MIN()&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x6EE1;&#x8DB3;WHERE&#x6761;&#x4EF6;&#x7684;&#x4E00;&#x5217;&#x7684;&#x6700;&#x5927;/&#x6700;&#x5C0F;&#x503C;</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> <span class="keyword">MAX</span>(&#x5217;&#x540D;) <span class="keyword">FROM</span> table_name</div><div class="line">  [<span class="keyword">WHERE</span> where_definition];</div></pre></td></tr></table></figure>
<h4 id="&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6392;&#x5E8F;"><a href="#&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6392;&#x5E8F;" class="headerlink" title="&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6392;&#x5E8F;"></a>&#x5BF9;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6392;&#x5E8F;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> colum1, colum2, colum3..</div><div class="line">    <span class="keyword">FROM</span> table_name</div><div class="line">    <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="keyword">colum</span> <span class="keyword">ASC</span>|<span class="keyword">DESC</span>;</div></pre></td></tr></table></figure>
<p>ORDER BY &#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x5217;&#xFF0C;&#x6392;&#x5E8F;&#x7684;&#x5217;&#x8868;&#x5373;&#x53EF;&#x4EE5;&#x662F;&#x8868;&#x4E2D;&#x7684;&#x5217;&#x540D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;SELECT&#x8BED;&#x53E5;&#x540E;&#x6307;&#x5B9A;&#x7684;&#x5217;&#x540D;.</p>
<ul>
<li>ASC &#x5347;&#x5E8F;&#xFF0C;DESC &#x964D;&#x5E8F;</li>
<li>ORDER BY &#x5B57;&#x53E5;&#x5E94;&#x8BE5;&#x4F4D;&#x4E8E;SELECT &#x8BED;&#x53E5;&#x7684;&#x7ED3;&#x5C3E;</li>
</ul>
<h4 id="&#x5206;&#x7EC4;&#x67E5;&#x8BE2;"><a href="#&#x5206;&#x7EC4;&#x67E5;&#x8BE2;" class="headerlink" title="&#x5206;&#x7EC4;&#x67E5;&#x8BE2;"></a>&#x5206;&#x7EC4;&#x67E5;&#x8BE2;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> colum1, colum2, ...</div><div class="line">  <span class="keyword">FROM</span> &#x8868;&#x540D;</div><div class="line">  <span class="keyword">LIMIT</span> [<span class="keyword">OFFSET</span>, ] &#x8BB0;&#x5F55;&#x6570;</div></pre></td></tr></table></figure>
<p>LIMIT&#x8868;&#x793A;&#x4ECE;&#x54EA;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#x5F00;&#x59CB;&#x5F80;&#x540E;&#x3010;&#x4E0D;&#x5305;&#x542B;&#x8BE5;&#x8BB0;&#x5F55;&#x3011;&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x5171;&#x67E5;&#x8BE2;&#x591A;&#x5C11;&#x8BB0;&#x5F55;</p>
<p>OFFSET&#x8868;&#x793A;&#x504F;&#x79FB;&#x91CF;:</p>
<ul>
<li>&#x5982;&#x679C;&#x4E3A;0&#x5219;&#x8868;&#x793A;&#x4ECE;&#x7B2C;&#x4E00;&#x6761;&#x8BB0;&#x5F55;&#x5F00;&#x59CB;</li>
<li>&#x5982;&#x679C;&#x4E3A;5&#x5219;&#x8868;&#x793A;&#x4ECE;&#x7B2C;6&#x6761;&#x8BB0;&#x5F55;&#x5F00;&#x59CB;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF1A;&#x5206;&#x9875;&#x67E5;&#x8BE2;</p>
<h4 id="&#x5206;&#x9875;&#x67E5;&#x8BE2;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;"><a href="#&#x5206;&#x9875;&#x67E5;&#x8BE2;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;" class="headerlink" title="&#x5206;&#x9875;&#x67E5;&#x8BE2;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;"></a>&#x5206;&#x9875;&#x67E5;&#x8BE2;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;</h4><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">-- &#x4EC5;&#x4EC5;&#x53D6;&#x4E86;&#x524D; 10 &#x6761;</div><div class="line">SELECET * FROM articles LIMIT 10</div><div class="line">-- &#x8DF3;&#x8FC7;&#x4E00;&#x6761;&#x53D6;&#x4E00;&#x6761;</div><div class="line">SELECET * FROM articles LIMIT 1, 1</div></pre></td></tr></table></figure>
<h3 id="&#x4E3A;&#x8868;&#x548C;&#x5B57;&#x6BB5;&#x533A;&#x522B;&#x540D;"><a href="#&#x4E3A;&#x8868;&#x548C;&#x5B57;&#x6BB5;&#x533A;&#x522B;&#x540D;" class="headerlink" title="&#x4E3A;&#x8868;&#x548C;&#x5B57;&#x6BB5;&#x533A;&#x522B;&#x540D;"></a>&#x4E3A;&#x8868;&#x548C;&#x5B57;&#x6BB5;&#x533A;&#x522B;&#x540D;</h3><h4 id="&#x4E3A;&#x8868;&#x53D6;&#x522B;&#x540D;"><a href="#&#x4E3A;&#x8868;&#x53D6;&#x522B;&#x540D;" class="headerlink" title="&#x4E3A;&#x8868;&#x53D6;&#x522B;&#x540D;"></a>&#x4E3A;&#x8868;&#x53D6;&#x522B;&#x540D;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> &#x8868;&#x522B;&#x540D;.id,&#x8868;&#x522B;&#x540D;.name... <span class="keyword">FROM</span> &#x8868;&#x540D; <span class="keyword">AS</span> &#x8868;&#x522B;&#x540D;</div><div class="line">  <span class="keyword">WHERE</span> &#x8868;&#x522B;&#x540D;.id = <span class="number">2.</span>.</div></pre></td></tr></table></figure>
<h4 id="&#x4E3A;&#x5B57;&#x6BB5;&#x53D6;&#x522B;&#x540D;"><a href="#&#x4E3A;&#x5B57;&#x6BB5;&#x53D6;&#x522B;&#x540D;" class="headerlink" title="&#x4E3A;&#x5B57;&#x6BB5;&#x53D6;&#x522B;&#x540D;"></a>&#x4E3A;&#x5B57;&#x6BB5;&#x53D6;&#x522B;&#x540D;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">SELECT</span> &#x5B57;&#x6BB5;&#x540D; [<span class="keyword">AS</span>] &#x522B;&#x540D; [,&#x5B57;&#x6BB5;&#x540D; [<span class="keyword">AS</span>] &#x522B;&#x540D;,...] <span class="keyword">FROM</span> &#x8868;&#x540D;;</div></pre></td></tr></table></figure>
<h3 id="&#x591A;&#x8868;&#x64CD;&#x4F5C;"><a href="#&#x591A;&#x8868;&#x64CD;&#x4F5C;" class="headerlink" title="&#x591A;&#x8868;&#x64CD;&#x4F5C;"></a>&#x591A;&#x8868;&#x64CD;&#x4F5C;</h3><p>&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x53EF;&#x80FD;&#x6709;&#x51E0;&#x5341;&#x5230;&#x51E0;&#x767E;&#x5F20;&#x8868;&#x4E0D;&#x7B49;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x5BF9;&#x591A;&#x5F20;&#x8868;&#x6765;&#x8FDB;&#x884C;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;&#xFF0C;&#x5BF9;&#x4E24;&#x5F20;&#x4EE5;&#x4E0A;&#x7684;&#x8868;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x5C31;&#x662F;&#x591A;&#x8868;&#x64CD;&#x4F5C;&#x3002;</p>
<h4 id="&#x5916;&#x952E;"><a href="#&#x5916;&#x952E;" class="headerlink" title="&#x5916;&#x952E;"></a>&#x5916;&#x952E;</h4><p>&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x6570;&#x636E;&#x7684;&#x5B8C;&#x6574;&#x6027;&#xFF0C;&#x5C06;&#x4E24;&#x5F20;&#x8868;&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x636E;&#x5EFA;&#x7ACB;&#x5173;&#x7CFB;&#xFF0C;&#x56E0;&#x6B64;&#x5C31;&#x9700;&#x8981;&#x5728;&#x6210;&#x7EE9;&#x8868;&#x4E2D;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;&#x3002;</p>
<ul>
<li>&#x5916;&#x952E;&#x662F;&#x6307;&#x5F15;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x8868;&#x4E2D;&#x7684;&#x4E00;&#x5217;&#x6216;&#x591A;&#x5217;&#xFF0C;&#x88AB;&#x5F15;&#x7528;&#x7684;&#x5217;&#x5E94;&#x8BE5;&#x5177;&#x6709;&#x4E3B;&#x952E;&#x7EA6;&#x675F;&#x6216;&#x552F;&#x4E00;&#x7EA6;&#x675F;&#x3002;</li>
<li>&#x5916;&#x952E;&#x7528;&#x4E8E;&#x5EFA;&#x7ACB;&#x548C;&#x52A0;&#x5F3A;&#x4E24;&#x4E2A;&#x8868;&#x6570;&#x636E;&#x4E4B;&#x95F4;&#x7684;&#x94FE;&#x63A5;&#x3002;</li>
</ul>
<h4 id="&#x4E3A;&#x8868;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;"><a href="#&#x4E3A;&#x8868;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;" class="headerlink" title="&#x4E3A;&#x8868;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;"></a>&#x4E3A;&#x8868;&#x6DFB;&#x52A0;&#x5916;&#x952E;&#x7EA6;&#x675F;</h4><p>&#x521B;&#x5EFA;&#x8868;&#x7684;&#x65F6;&#x5019;&#x6DFB;&#x52A0;&#x5916;&#x952E;: </p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> department(</div><div class="line">    <span class="keyword">id</span> <span class="built_in">INT</span> PRIMARY <span class="keyword">KEY</span> auto_increment,</div><div class="line">    <span class="keyword">name</span> <span class="built_in">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span></div><div class="line">);</div><div class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> employee(</div><div class="line">    <span class="keyword">id</span> <span class="built_in">INT</span> PRIMARY <span class="keyword">KEY</span> auto_increment,</div><div class="line">    <span class="keyword">name</span> <span class="built_in">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</div><div class="line">    dept_id <span class="built_in">INT</span>,</div><div class="line">    FOREIGN <span class="keyword">KEY</span> (<span class="keyword">id</span>) <span class="keyword">REFERENCES</span> department(<span class="keyword">id</span>)</div><div class="line">);</div></pre></td></tr></table></figure>
<p>&#x8868;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#xFF0C;&#x901A;&#x8FC7;&#x4FEE;&#x6539;&#x8868;&#x7684;&#x8BED;&#x53E5;&#x589E;&#x52A0;&#x5916;&#x952E;:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> &#x8868;&#x540D; <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> &#x5916;&#x952E;&#x540D; FOREIGN <span class="keyword">KEY</span>(&#x5916;&#x952E;&#x5B57;&#x6BB5;&#x540D;) <span class="keyword">REFERENCES</span> &#x5916;&#x8868;&#x8868;&#x540D;(&#x4E3B;&#x952E;&#x5B57;&#x6BB5;&#x540D;);</div></pre></td></tr></table></figure>
<h4 id="&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;"><a href="#&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;" class="headerlink" title="&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;"></a>&#x5220;&#x9664;&#x5916;&#x952E;&#x7EA6;&#x675F;</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> &#x8868;&#x540D; <span class="keyword">DROP</span> FOREIGN <span class="keyword">KEY</span> &#x5916;&#x952E;&#x540D;;</div></pre></td></tr></table></figure>
<h3 id="&#x64CD;&#x4F5C;&#x5173;&#x8054;&#x8868;"><a href="#&#x64CD;&#x4F5C;&#x5173;&#x8054;&#x8868;" class="headerlink" title="&#x64CD;&#x4F5C;&#x5173;&#x8054;&#x8868;"></a>&#x64CD;&#x4F5C;&#x5173;&#x8054;&#x8868;</h3><blockquote>
<p>&#x5173;&#x8054;&#x5173;&#x7CFB;:</p>
</blockquote>
<ul>
<li>&#x591A;&#x5BF9;&#x4E00;</li>
<li>&#x591A;&#x5BF9;&#x591A;</li>
<li>&#x4E00;&#x5BF9;&#x4E00;</li>
</ul>
<h2 id="&#x4F7F;&#x7528;-Node-&#x64CD;&#x4F5C;-MySQL-&#x6570;&#x636E;&#x5E93;"><a href="#&#x4F7F;&#x7528;-Node-&#x64CD;&#x4F5C;-MySQL-&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x4F7F;&#x7528; Node &#x64CD;&#x4F5C; MySQL &#x6570;&#x636E;&#x5E93;"></a>&#x4F7F;&#x7528; Node &#x64CD;&#x4F5C; MySQL &#x6570;&#x636E;&#x5E93;</h2><p>&#x5B89;&#x88C5;&#xFF1A;</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ npm install --save mysql</div></pre></td></tr></table></figure>
<p><a href="https://www.npmjs.com/package/mysql" target="_blank" rel="external">&#x4F7F;&#x7528; Node &#x64CD;&#x4F5C;</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>);</div><div class="line"><span class="comment">// &#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;</span></div><div class="line"><span class="keyword">var</span> connection = mysql.createConnection({</div><div class="line">  <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">  <span class="attr">port</span>: <span class="number">3306</span>,</div><div class="line">  <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">  <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">  <span class="attr">database</span>: <span class="string">&apos;personal&apos;</span></div><div class="line">});</div><div class="line"><span class="comment">// &#x5F00;&#x59CB;&#x64CD;&#x4F5C;&#x4E4B;&#x524D;&#x5148;&#x8FDE;&#x63A5;&#xFF0C;&#x8FD9;&#x4E00;&#x6B65;&#x53EF;&#x4EE5;&#x7701;&#x7565;</span></div><div class="line"><span class="comment">// &#x5F53;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x8FDE;&#x63A5;</span></div><div class="line"><span class="comment">// connection.connect();</span></div><div class="line"><span class="comment">// &#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;</span></div><div class="line"><span class="comment">// &#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x4EFB;&#x4F55; SQL &#x8BED;&#x8A00;&#xFF0C;&#x589E;&#x5220;&#x6539;&#x67E5;&#xFF0C;? &#x662F;&#x8981;&#x62FC;&#x63A5;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x6570;&#x636E;</span></div><div class="line"><span class="comment">// &#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x9009;&#xFF0C;&#x4F20;&#x5165;&#x6570;&#x636E;&#x6570;&#x7EC4;</span></div><div class="line"><span class="comment">// &#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span></div><div class="line"><span class="comment">//    &#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x5F02;&#x5E38;&#x9519;&#x8BEF;&#x5BF9;&#x8C61;</span></div><div class="line"><span class="comment">//    &#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x64CD;&#x4F5C;&#x7ED3;&#x679C;</span></div><div class="line"><span class="comment">//    &#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#xFF0C;&#x57FA;&#x672C;&#x4E0D;&#x8003;&#x8651;</span></div><div class="line">connection.query(<span class="string">&apos;SELECT * FROM `users` WHERE `age`&gt;?&apos;</span>, [<span class="number">20</span>], <span class="function"><span class="keyword">function</span>(<span class="params">err, rows, fields</span>) </span>{</div><div class="line">  <span class="keyword">if</span> (err) <span class="keyword">throw</span> err;</div><div class="line">  <span class="comment">// &#x5982;&#x679C;&#x662F; SELECT&#xFF0C;&#x7ED3;&#x679C;&#x662F;&#x6570;&#x7EC4;</span></div><div class="line">  <span class="built_in">console</span>.log(<span class="string">&apos;The solution is: &apos;</span>, rows[<span class="number">0</span>].solution);</div><div class="line">  <span class="comment">// &#x5BF9;&#x8C61;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5F88;&#x91CD;&#x8981;&#xFF1A;affectedRows</span></div><div class="line">  <span class="built_in">console</span>.log(<span class="string">`&#x53D7;&#x5F71;&#x54CD;&#x7684;&#x884C;&#x6570;&#xFF1A;<span class="subst">${rows.affectedRows}</span>`</span>);</div><div class="line">});</div><div class="line"><span class="comment">// &#x5173;&#x95ED;&#x6570;&#x636E;&#x5E93;</span></div><div class="line"><span class="comment">// &#x4E00;&#x822C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x64CD;&#x4F5C;&#x4E00;&#x6B21;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x64CD;&#x4F5C;&#x5B8C;&#x6BD5;&#xFF0C;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;</span></div><div class="line">connection.end();</div></pre></td></tr></table></figure>
<p>&#x4F7F;&#x7528; Node &#x64CD;&#x4F5C;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>)</div><div class="line">exports.query = <span class="function">(<span class="params">sql, params = [], callback</span>) =&gt;</span> {</div><div class="line">  <span class="keyword">const</span> connection = mysql.createConnection({</div><div class="line">    <span class="attr">connectionLimit</span> : <span class="number">500</span>,</div><div class="line">    <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">    <span class="attr">port</span>: <span class="number">3306</span>,</div><div class="line">    <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">    <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">    <span class="attr">database</span>: <span class="string">&apos;personal&apos;</span></div><div class="line">  })</div><div class="line">  connection.query(sql, params, (err, rows) =&gt; {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> callback(err)</div><div class="line">    }</div><div class="line">    callback(<span class="literal">null</span>, rows)</div><div class="line">    <span class="comment">// &#x64CD;&#x4F5C;&#x5B8C;&#x6BD5;&#xFF0C;&#x7ED3;&#x675F;&#x8FDE;&#x63A5;</span></div><div class="line">    connection.end()</div><div class="line">  })</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;&#x64CD;&#x4F5C;-MySQL-&#x6570;&#x636E;&#x5E93;"><a href="#&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;&#x64CD;&#x4F5C;-MySQL-&#x6570;&#x636E;&#x5E93;" class="headerlink" title="&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;&#x64CD;&#x4F5C; MySQL &#x6570;&#x636E;&#x5E93;"></a>&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;&#x64CD;&#x4F5C; MySQL &#x6570;&#x636E;&#x5E93;</h3><ul>
<li>&#x4FEE;&#x6539;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E0B; my.ini &#x6587;&#x4EF6;&#x4E2D;&#x7684;: <code>max_connections=1000</code> &#x9ED8;&#x8BA4;&#x662F; <code>max_connections=151</code></li>
<li>&#x91CD;&#x542F;&#x670D;&#x52A1;&#x5668;</li>
</ul>
<p><img src="/2016/10/27/s011-&#x6570;&#x636E;&#x5E93;-MySQL/&#x8FDE;&#x63A5;&#x6C60;.png" alt="connection-pool"></p>
<h3 id="&#x8FDE;&#x63A5;&#x6C60;"><a href="#&#x8FDE;&#x63A5;&#x6C60;" class="headerlink" title="&#x8FDE;&#x63A5;&#x6C60;"></a>&#x8FDE;&#x63A5;&#x6C60;</h3><p>&#x5C01;&#x88C5;&#x8FC7;&#x7A0B;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>);</div><div class="line"></div><div class="line"><span class="comment">// &#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#xFF0C;&#x63D0;&#x9AD8;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x6548;&#x7387;</span></div><div class="line"><span class="comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8FDE;&#x63A5;&#x6C60;&#xFF0C;&#x6C60;&#x5B50;&#x5B58;&#x653E;&#x7684;&#x8FDE;&#x63A5;&#x6570;&#x91CF;&#x662F; 100 &#x4E2A;</span></div><div class="line"><span class="keyword">const</span> pool = mysql.createPool({</div><div class="line">  <span class="attr">connectionLimit</span>: <span class="number">100</span>,</div><div class="line">  <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">  <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">  <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">  <span class="attr">database</span>: <span class="string">&apos;personal&apos;</span></div><div class="line">});</div><div class="line"></div><div class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++) {</div><div class="line">  <span class="comment">// &#x4ECE;&#x6C60;&#x5B50;&#x4E2D;&#x62FF;&#x4E00;&#x4E2A;&#x53EF;&#x7528;&#x7684;&#x8FDE;&#x63A5;</span></div><div class="line">  pool.getConnection(<span class="function">(<span class="params">err, connection</span>) =&gt;</span> {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">throw</span> err;</div><div class="line">    }</div><div class="line">    connection.query(<span class="string">&apos;INSERT INTO `feedback`(`message`, `name`, `email`, `date`) VALUES(?, ?, ?, ?)&apos;</span>, [</div><div class="line">      <span class="string">&apos;&#x4ECA;&#x5929;&#x7684;&#x96FE;&#x973E;&#x5F88;&#x9187;&#x539A;&apos;</span>,</div><div class="line">      <span class="string">&apos;&#x6821;&#x957F;&apos;</span>,</div><div class="line">      <span class="string">&apos;xiaozhang@abc.com&apos;</span>,</div><div class="line">      <span class="string">&apos;2016-11-17 09:31:00&apos;</span></div><div class="line">    ], (err, stat) =&gt; {</div><div class="line">      <span class="comment">// &#x5C3D;&#x65E9;&#x7684;&#x91CA;&#x653E;&#x56DE;&#x8FDE;&#x63A5;&#x6C60;</span></div><div class="line">      <span class="comment">// &#x53EA;&#x8981;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x8BF4;&#x660E;&#x8FD9;&#x4E2A;&#x8FDE;&#x63A5;&#x7684;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x4E86;</span></div><div class="line">      connection.release();</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">throw</span> err;</div><div class="line">      }</div><div class="line">      <span class="built_in">console</span>.log(<span class="string">`&#x7B2C;<span class="subst">${i+<span class="number">1</span>}</span>&#x4E2A;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x4E86;`</span>);</div><div class="line">    });</div><div class="line">  });</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x5C01;&#x88C5;&#xFF1A;db.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>);</div><div class="line"></div><div class="line"><span class="keyword">const</span> pool = mysql.createPool({</div><div class="line">  <span class="attr">connectionLimit</span>: <span class="number">100</span>,</div><div class="line">  <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">  <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">  <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">  <span class="attr">database</span>: <span class="string">&apos;personal&apos;</span></div><div class="line">});</div><div class="line"></div><div class="line"><span class="comment">// rest &#x53C2;&#x6570;</span></div><div class="line"><span class="comment">// &#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x51FA;&#x73B0;&#xFF0C;&#x4EE5; ... &#x5F00;&#x5934;&#xFF0C;&#x540E;&#x9762;&#x8DDF;&#x4E00;&#x4E2A;&#x540D;&#x5B57;</span></div><div class="line"><span class="comment">// rest &#x53C2;&#x6570;&#x5C31;&#x4EE3;&#x66FF;&#x4E86; arguments</span></div><div class="line">exports.query = <span class="function"><span class="keyword">function</span> (<span class="params">sql, ...values</span>) </span>{</div><div class="line">  <span class="keyword">let</span> callback;</div><div class="line">  <span class="keyword">let</span> params = [];</div><div class="line">  <span class="keyword">if</span> (values.length === <span class="number">3</span>) {</div><div class="line">    params = values[<span class="number">0</span>];</div><div class="line">    callback = values[<span class="number">1</span>];</div><div class="line">  } <span class="keyword">else</span> <span class="keyword">if</span> (values.length === <span class="number">2</span>) {</div><div class="line">    callback = values[<span class="number">0</span>];</div><div class="line">  }</div><div class="line">  pool.getConnection(<span class="function">(<span class="params">err, connection</span>) =&gt;</span> {</div><div class="line">    <span class="keyword">if</span> (err) {</div><div class="line">      <span class="keyword">return</span> callback(err);</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x5982;&#x679C;&#x4F20;&#x9012;&#x4E86;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F; callback</span></div><div class="line">    <span class="comment">// &#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;params &#x5C31;&#x662F; callback</span></div><div class="line">    <span class="comment">// &#x540E;&#x9762;&#x7684; &#x53C2;&#x6570;&#x5C31;&#x5FFD;&#x7565;&#x4E0D;&#x8BA1;&#x4E86;</span></div><div class="line">    <span class="comment">// &#x5982;&#x679C;&#x771F;&#x7684;&#x4F20;&#x9012;&#x4E86;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x5C31;&#x662F;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;</span></div><div class="line">    connection.query(sql, params, (err, result) =&gt; {</div><div class="line">      connection.release();</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> callback(err);</div><div class="line">      }</div><div class="line">      callback(<span class="literal">null</span>, result);</div><div class="line">    });</div><div class="line">  });</div><div class="line">};</div></pre></td></tr></table></figure>
<h3 id="promise-&#x7248;"><a href="#promise-&#x7248;" class="headerlink" title="promise &#x7248;"></a>promise &#x7248;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> mysql = <span class="built_in">require</span>(<span class="string">&apos;mysql&apos;</span>);</div><div class="line"></div><div class="line"><span class="keyword">const</span> pool = mysql.createPool({</div><div class="line">  <span class="attr">connectionLimit</span>: <span class="number">100</span>,</div><div class="line">  <span class="attr">host</span>: <span class="string">&apos;localhost&apos;</span>,</div><div class="line">  <span class="attr">user</span>: <span class="string">&apos;root&apos;</span>,</div><div class="line">  <span class="attr">password</span>: <span class="string">&apos;*********&apos;</span>,</div><div class="line">  <span class="attr">database</span>: <span class="string">&apos;personal&apos;</span></div><div class="line">});</div><div class="line"></div><div class="line">exports.query = <span class="function">(<span class="params">sql, params = []</span>) =&gt;</span> {</div><div class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> {</div><div class="line">    pool.getConnection(<span class="function">(<span class="params">err, connection</span>) =&gt;</span> {</div><div class="line">      <span class="keyword">if</span> (err) {</div><div class="line">        <span class="keyword">return</span> reject(err);</div><div class="line">      }</div><div class="line">      connection.query(sql, params, (err, result) =&gt; {</div><div class="line">        connection.release();</div><div class="line">        <span class="keyword">if</span> (err) {</div><div class="line">          <span class="keyword">return</span> reject(err);</div><div class="line">        }</div><div class="line">        resolve(result);</div><div class="line">      });</div><div class="line">    });</div><div class="line">  });</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x8C03;&#x7528;&#x793A;&#x4F8B;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> db = <span class="built_in">require</span>(<span class="string">&apos;./db&apos;</span>)</div><div class="line"></div><div class="line">db.query(<span class="string">&apos;SELECT 1 + 1 as solution&apos;</span>);</div><div class="line">  .then(<span class="function"><span class="params">rows</span> =&gt;</span> {</div><div class="line">    <span class="comment">// use rows</span></div><div class="line">    <span class="keyword">return</span> db.query(<span class="string">&apos;INSERT INTO table_name VALUES(?, ?, ?)&apos;</span>, [<span class="string">&apos;&#x503C;1&apos;</span>, <span class="string">&apos;&#x503C;2&apos;</span>, <span class="string">&apos;&#x503C;3&apos;</span>])</div><div class="line">  })</div><div class="line">  .then(<span class="function"><span class="params">rows</span> =&gt;</span> {</div><div class="line">    <span class="comment">// use rows</span></div><div class="line">  })</div><div class="line">  .catch(<span class="function"><span class="params">err</span> =&gt;</span> {</div><div class="line">    <span class="comment">// handle error</span></div><div class="line">  });</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Node-js/" rel="tag">#Node.js</a>
          
            <a href="/tags/数据库/" rel="tag">#数据库</a>
          
            <a href="/tags/MySQL/" rel="tag">#MySQL</a>
          
            <a href="/tags/MongoDB/" rel="tag">#MongoDB</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/10/23/s010-node-相册小项目（下）/" rel="next" title="Node-相册小项目（下）">
                <i class="fa fa-chevron-left"></i> Node-相册小项目（下）
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/01/s012-Cookie、Session-存储/" rel="prev" title="Cookie、Session 存储技术">
                Cookie、Session 存储技术 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/10/27/s011-数据库-MySQL/"
     data-title="数据库基础 以及 Node 操作方法"
     data-content=""
     data-url="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/10/27/s011-数据库-MySQL/"
           data-title="数据库基础 以及 Node 操作方法" data-url="https://hongqinma.github.io/2016/10/27/s011-数据库-MySQL/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库介绍"><span class="nav-number">1.</span> <span class="nav-text">数据库介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MongoDB"><span class="nav-number">2.</span> <span class="nav-text">MongoDB</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建数据目录并启动MongoDB数据库服务器"><span class="nav-number">2.1.</span> <span class="nav-text">创建数据目录并启动MongoDB数据库服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接-MongoDB-数据库"><span class="nav-number">2.2.</span> <span class="nav-text">连接 MongoDB 数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#存储结构"><span class="nav-number">2.3.</span> <span class="nav-text">存储结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基本操作"><span class="nav-number">2.4.</span> <span class="nav-text">基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在-Node-中操作-MongoDB"><span class="nav-number">2.5.</span> <span class="nav-text">在 Node 中操作 MongoDB</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装和配置-MySQL-数据库"><span class="nav-number">3.</span> <span class="nav-text">安装和配置 MySQL 数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库的存储结构"><span class="nav-number">4.</span> <span class="nav-text">数据库的存储结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建数据库和表"><span class="nav-number">5.</span> <span class="nav-text">创建数据库和表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建数据库"><span class="nav-number">5.1.</span> <span class="nav-text">创建数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看数据库"><span class="nav-number">5.2.</span> <span class="nav-text">查看数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除数据库"><span class="nav-number">5.3.</span> <span class="nav-text">删除数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#选择数据库"><span class="nav-number">5.4.</span> <span class="nav-text">选择数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看当前使用的数据库"><span class="nav-number">5.5.</span> <span class="nav-text">查看当前使用的数据库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据类型"><span class="nav-number">6.</span> <span class="nav-text">数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#整数类型"><span class="nav-number">6.1.</span> <span class="nav-text">整数类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浮点型"><span class="nav-number">6.2.</span> <span class="nav-text">浮点型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#时间和日期类型"><span class="nav-number">6.3.</span> <span class="nav-text">时间和日期类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CHAR和VARCHAR"><span class="nav-number">6.4.</span> <span class="nav-text">CHAR和VARCHAR</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基本操作-1"><span class="nav-number">7.</span> <span class="nav-text">基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#查看表结构"><span class="nav-number">7.1.</span> <span class="nav-text">查看表结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改数据表"><span class="nav-number">7.2.</span> <span class="nav-text">修改数据表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除数据表"><span class="nav-number">7.3.</span> <span class="nav-text">删除数据表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表的约束"><span class="nav-number">7.4.</span> <span class="nav-text">表的约束</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#主键约束"><span class="nav-number">7.4.1.</span> <span class="nav-text">主键约束</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#非空约束"><span class="nav-number">7.4.2.</span> <span class="nav-text">非空约束</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#唯一约束"><span class="nav-number">7.4.3.</span> <span class="nav-text">唯一约束</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#默认约束"><span class="nav-number">7.4.4.</span> <span class="nav-text">默认约束</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置表的字段值自动增加"><span class="nav-number">7.5.</span> <span class="nav-text">设置表的字段值自动增加</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加、更新与删除数据"><span class="nav-number">8.</span> <span class="nav-text">添加、更新与删除数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#添加数据"><span class="nav-number">8.1.</span> <span class="nav-text">添加数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#为表中所有字段添加数据"><span class="nav-number">8.1.1.</span> <span class="nav-text">为表中所有字段添加数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#按照指定列添加数据："><span class="nav-number">8.1.2.</span> <span class="nav-text">按照指定列添加数据：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#同时添加多条记录"><span class="nav-number">8.1.3.</span> <span class="nav-text">同时添加多条记录</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新全部数据："><span class="nav-number">8.2.</span> <span class="nav-text">更新全部数据：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#按条件更新："><span class="nav-number">8.2.1.</span> <span class="nav-text">按条件更新：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#条件可以使用的运算符："><span class="nav-number">8.2.2.</span> <span class="nav-text">条件可以使用的运算符：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除数据："><span class="nav-number">8.3.</span> <span class="nav-text">删除数据：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#删除全部数据"><span class="nav-number">8.3.1.</span> <span class="nav-text">删除全部数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#根据条件删除："><span class="nav-number">8.3.2.</span> <span class="nav-text">根据条件删除：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#初始化"><span class="nav-number">8.3.3.</span> <span class="nav-text">初始化</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#单表查询"><span class="nav-number">8.4.</span> <span class="nav-text">单表查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#简单查询"><span class="nav-number">8.4.1.</span> <span class="nav-text">简单查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查询表中所有数据："><span class="nav-number">8.4.2.</span> <span class="nav-text">查询表中所有数据：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#按照指定列查询表中所有数据："><span class="nav-number">8.4.3.</span> <span class="nav-text">按照指定列查询表中所有数据：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#根据条件查询数据："><span class="nav-number">8.4.4.</span> <span class="nav-text">根据条件查询数据：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#多表查询："><span class="nav-number">8.4.5.</span> <span class="nav-text">多表查询：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查询总记录数："><span class="nav-number">8.4.6.</span> <span class="nav-text">查询总记录数：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#聚合函数"><span class="nav-number">8.4.7.</span> <span class="nav-text">聚合函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#对查询结果排序"><span class="nav-number">8.4.8.</span> <span class="nav-text">对查询结果排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分组查询"><span class="nav-number">8.4.9.</span> <span class="nav-text">分组查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#分页查询一个例子"><span class="nav-number">8.4.10.</span> <span class="nav-text">分页查询一个例子</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为表和字段区别名"><span class="nav-number">8.5.</span> <span class="nav-text">为表和字段区别名</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#为表取别名"><span class="nav-number">8.5.1.</span> <span class="nav-text">为表取别名</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#为字段取别名"><span class="nav-number">8.5.2.</span> <span class="nav-text">为字段取别名</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多表操作"><span class="nav-number">8.6.</span> <span class="nav-text">多表操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#外键"><span class="nav-number">8.6.1.</span> <span class="nav-text">外键</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#为表添加外键约束"><span class="nav-number">8.6.2.</span> <span class="nav-text">为表添加外键约束</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除外键约束"><span class="nav-number">8.6.3.</span> <span class="nav-text">删除外键约束</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#操作关联表"><span class="nav-number">8.7.</span> <span class="nav-text">操作关联表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用-Node-操作-MySQL-数据库"><span class="nav-number">9.</span> <span class="nav-text">使用 Node 操作 MySQL 数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用连接池操作-MySQL-数据库"><span class="nav-number">9.1.</span> <span class="nav-text">使用连接池操作 MySQL 数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接池"><span class="nav-number">9.2.</span> <span class="nav-text">连接池</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#promise-版"><span class="nav-number">9.3.</span> <span class="nav-text">promise 版</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
